PHP move_uploaded_file() Function Last Updated : 28 Apr, 2025 Summarize Suggest changes Share Like Article Like Report The move_uploaded_file() function is an inbuilt function in PHP that is used to change the file destination. This function only works when the file is uploaded by the PHP POST function. If the file is valid it will uploaded. Syntax: move_uploaded_file( string $from, string $to ): boolParameters: This function accepts two parameters that are described below. $from: This parameter specifies the temporary location of the file. This is the location where your uploaded file is stored temporarily.$to: This parameter specifies the destination of the location where your file will be stored.Return Value: This function returns true if the function successfully changes the location of the file otherwise this function will return false. Program 1: The following program demonstrates the move_uploaded_file() function. PHP /* upload.php */ <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { if ( isset($_FILES["file_upload"]) && $_FILES["file_upload"]["error"] == UPLOAD_ERR_OK ) { // Add name of upload directory $uploadDirectory = "./uploads/"; if (move_uploaded_file( $_FILES["file_upload"]["tmp_name"], $uploadDirectory . $_FILES["file_upload"]["name"] )) { echo "File uploaded successfully!"; } else { echo "Error moving file."; } } else { echo "Error uploading file."; } } ?> HTML <!-- index.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Simple File Upload</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file_upload"> Choose a file: </label> <input type="file" name="file_upload" id="file_upload"> <input type="submit" value="Upload File"> </form> </body> </html> Output: Program 2: The following program demonstrates the move_uploaded_file() function. In this example we are uploading only specific extensions. PHP /* upload.php */ <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { if ( isset($_FILES["file_upload"]) && $_FILES["file_upload"]["error"] == UPLOAD_ERR_OK ) { $uploadDirectory = "uploads/"; $originalFileName = basename($_FILES["file_upload"]["name"]); $fileExtension = pathinfo( $originalFileName, PATHINFO_EXTENSION); $allowedExtensions = ["jpg", "jpeg", "png"]; if (in_array($fileExtension, $allowedExtensions)) { if ( move_uploaded_file( $_FILES["file_upload"]["tmp_name"], $uploadDirectory . $originalFileName ) ) { echo "File uploaded successfully!"; } else { echo "Error moving file."; } } else { echo "Error: Only JPG, JPEG, and " . "PNG files are allowed."; } } else { echo "Error uploading file."; } } ?> HTML <!-- index.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>File Upload Example</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file_upload"> Choose a file (only JPG, JPEG, and PNG): </label> <input type="file" name="file_upload" id="file_upload" accept=".jpg, .jpeg, .png"> <input type="submit" value="Upload File"> </form> </body> </html> Output: Reference: https://www.php.net/manual/en/function.move-uploaded-file.php Advertise with us Next Article PHP | fileatime( ) Function N neeraj3304 Follow Similar Reads PHP | is_uploaded_file( ) Function The is_uploaded_file() function in PHP is an inbuilt function which is used to check whether the specified file uploaded via HTTP POST or not. The name of the file is sent as a parameter to the is_uploaded_file() function and it returns True if the file is uploaded via HTTP POST. This function can b 2 min read PHP is_file( ) Function The is_file() function in PHP is an inbuilt function which is used to check whether the specified file is a regular file or not. The name of the file is sent as a parameter to the is_file() function and it returns True if the file is a regular file else it returns False. Syntax: bool is_file($file) 2 min read PHP | filesize( ) Function The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure. The result of the filesize() function is cached and a funct 2 min read PHP | filetype( ) Function The filetype() function in PHP is an inbuilt function which is used to return the file type of a specified file or a directory. The filetype() function accepts the filename as a parameter and returns one of the seven file types on success and False on failure. The seven possible return values of the 2 min read PHP | fileatime( ) Function The fileatime() function in PHP is an inbuilt function which is used to return the last access time of a specified file. The fileatime() function returns the last access time of a file as a Unix Timestamp on success and False on failure. The filename is passed as a parameter to the fileatime() funct 2 min read PHP | filemtime( ) Function The filemtime() function in PHP is an inbuilt function which is used to return the last time of a specified file when its content was modified. The filemtime() function returns the last time the file was changed as a Unix Timestamp on success and False on failure. The filename is passed as a paramet 2 min read Article Tags : PHP PHP-function PHP-Filesystem Like