How to move a file into a different folder on the server using PHP? Last Updated : 24 May, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: 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. Upload.html HTML <!DOCTYPE html> <html> <head> <title> Move a file into a different folder on the server </title> </head> <body> <form action="upfile.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file"> <br><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> upfile.php php <?php // The target directory of uploading is uploads $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["file"]["name"]); $uOk = 1; if(isset($_POST["submit"])) { // Check if file already exists if (file_exists($target_file)) { echo "file already exists.<br>"; $uOk = 0; } // Check if $uOk is set to 0 if ($uOk == 0) { echo "Your file was not uploaded.<br>"; } // if uOk=1 then try to upload file else { // $_FILES["file"]["tmp_name"] implies storage path // in tmp directory which is moved to uploads // directory using move_uploaded_file() method if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["file"]["name"]) . " has been uploaded.<br>"; // Moving file to New directory if(rename($target_file, "New/". basename( $_FILES["file"]["name"]))) { echo "File moving operation success<br>"; } else { echo "File moving operation failed..<br>"; } } else { echo "Sorry, there was an error uploading your file.<br>"; } } } ?> 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: Code running with use of rename method (Moving to New) Important methods: 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. Comment More infoAdvertise with us Next Article How to move a file into a different folder on the server using PHP? piyush25pv Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to get the current file name using PHP script ? In this article, we will learn how to get the current filename using PHP. Input : c:/xampp/htdocs/project/home.php Output : project/home.php Input : c:/xampp/htdocs/project/abc.txt Output : project/abc.txt Sometimes, we need to get the current file name with the directory name in which our page is s 2 min read How to get names of all the subfolders and files present in a directory using PHP? Given the path of the folder and the task is to print the names of subfolders and files present inside them. Explanation: In our PHP code, initially, it is checked whether provided path or filename is a directory or not using the is_dir() function. Now, we open the directory using opendir() function 2 min read How to copy a file from one directory to another using PHP ? The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bool 2 min read How to append data in JSON file through HTML form using PHP ? The purpose of this article is to append data to a JSON file through HTML form using PHP. Approach 1: If the JSON file is not created then we create a new JSON file, send data to it, and append data in it. To see how to create a JSON file by taking data from the HTML form, refer this link. Approach 4 min read Deleting all files from a folder using PHP In PHP, files from a folder can be deleted using various approaches and inbuilt methods such as unlink, DirectoryIterator and DirectoryRecursiveIterator. Some of these approaches are explained below: Approach 1: Generate a list of files using glob() method Iterate over the list of files. Check wheth 3 min read How to count files in a directory using PHP? PHP contains many functions like count(), iterator_count(), glob(), openddir(), readdir(), scandir() and FilesystemIterator() to count number of files in a directory.count() Function: The count() functions is an array function which is used to count all elements in an array or something in an object 3 min read How to get the file size using PHP ? In this article, we are going to discuss how to get the file size using PHP. PHP is a general-purpose scripting language that is suited for both server and client scripting language for website development. To get the file size, we will use filesize() function. The filesize() function returns the s 1 min read How to delete a file using PHP ? To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context. Syntax: unlink( $filename, 2 min read How to post data using file_get_contents in PHP ? The file_get_contents() function in PHP is used to read the contents of a file and make HTTP requests using GET and get HTTP responses using POST methods. The HTTP POST request can be made using the $context parameter of the file_get_contents() function, which posts the specified data to the URL spe 3 min read How to read data from a file stored in XAMPP webserver using PHP ? We have given a file stored on XAMPP server and the task is to read the file from server and display the file content on the screen using PHP. We use some PHP functions to solve this problem. File: A file is set of data stored in a disk in different formats. For example - .txt, .exe, .pdf etc fopen 2 min read Like