The move_uploaded_file() function and rename() function is used to move a file into a different folder on the server. In this case, we have a file already uploaded in the temp directory of server from where the new directory is assigned by the method. The file temp is fully moved to a new location. The move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved. Thus to move an already uploaded file we use the rename() method.
move_uploaded_file ( string $Sourcefilename, string $destination ) : bool rename ( string $oldname, string $newname [, resource $context ] ) : bool
- move_upload_file() method: This function checks to ensure that the source file or ‘$Sourcefilename’ in the syntax is a valid upload file (meaning that it was uploaded via PHP’s HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination or ‘$destination’ in the syntax.
The sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system. Note that if in case the destination file already exists, it will be overwritten. Due to this reason, a file should be checked first for its availability and then the only action must be taken.
- rename() method: This method attempts to rename oldname to newname, moving it between directories if necessary. If newname file exists then it will be overwritten. If renaming newname directory exists then this function will emit a warning.
Example: This example is a code which uploads a file in a directory names Uploads and then it changes its path to another directory named as New.
Note: The directories Uploads and New are already existing once and thus you will have to make them if they are not available inside the server.
Code running with use of rename method (Moving to New)
- file_exists($target_file): This method is used to check the existence of path. If it exists then it returns true else it returns false.
- basename( $_FILES[“file”][“name”] ): This method is used to get the name of the chosen file and its specialty lies in the fact that it operates on the input string provided by the user and is unaware of the actual file system and provides usage of security feature provided by the browsers.
- Deleting all files from a folder using PHP
- How to link back out of a folder using the a-href tag?
- Difference Between Web server and Application server
- Difference between Apache Tomcat server and Apache web server
- How to make an HTML link to open a folder?
- How to see the changes in whole directory/folder containing many sass files ?
- How to Hide a Folder in Windows?
- How to copy folder recursively in Node.js ?
- Why does SASS cache folder is created ?
- How to insert a whole folder in New Google Sites ?
- jQuery | Move an element into another element
- Build a Simple static file web server in Node
- How to log errors and warnings into a file in php?
- How to convert XML file into array in PHP?
- How to get file input by selected file name without path using jQuery ?
- How to import data from .CSV file into MySQL table using Node.js ?
- PHP program to fetch data from localhost server database using XAMPP
- Create a web server and run PHP script on it using Raspberry Pi
- PHP | Program to move (key,value) pair upwards or downwards
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.