7.7 C
New York
Saturday, February 24, 2024

PHP move_uploaded_file() Function


The move_uploaded_file() is a PHP function that is used to move an uploaded file to a new destination.


  • Before moving the uploaded file to a new location, this function first checks if the file is a valid upload or not (i.e. uploaded using HTTP POST mechanism).
  • Therefore we can say this function only works if the file is uploaded using HTTP POST mechanism.
  • If the desitnation file already exists, it will be overwritten.
  • This function works with PHP version 4.0.3 and above.


move_uploaded_file($file_path, $destination)


Parameter Type Description
file_path (mandatory) string specify the location of the file to be moved
destination (mandatory) string specifies the new location for the uploaded file

Return Value

move_uploaded_file function returns boolean value ie:

  • Returns TRUE if success 
  • Returns FALSE if the file is not a valid upload
  • Returns FALSE along with warning message if the file is valid upload but cannot be moved due to any issues.


Program to show use of move_uploaded_file() in PHP

HTML File Code

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>File Upload and move to diffrent Location</title>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file" id="file">
        <button type="submit" name="submit">UPLOAD FILE</button>

PHP File 

//Checking weather it is a valid request or not
    //Taking the files from input
    $file = $_FILES['file'];
    //Getting the file name of the uploaded file
    $fileName = $_FILES['file']['name'];
    //Getting the Temporary file name of the uploaded file
    $fileTempName = $_FILES['file']['tmp_name'];
    //Getting the file size of the uploaded file
    $fileSize = $_FILES['file']['size'];
    //getting the no. of error in uploading the file
    $fileError = $_FILES['file']['error'];
    //Getting the file type of the uploaded file
    $fileType = $_FILES['file']['type'];

    //Getting the file ext
    $fileExt = explode('.',$fileName);
    $fileActualExt = strtolower(end($fileExt));

    //Array of Allowed file type
    $allowedExt = array("jpg","jpeg","png","pdf");

    //Checking, Is file extentation is in allowed extentation array
    if(in_array($fileActualExt, $allowedExt)){
        //Checking, Is there any file error
        if($fileError == 0){
            //Checking,The file size is bellow than the allowed file size
            if($fileSize < 10000000){
                //Creating a unique name for file
                $fileNemeNew = uniqid('',true).".".$fileActualExt;
                //File destination
                $fileDestination = 'Uploads/'.$fileNemeNew;
                //function to move temp location to permanent location
                move_uploaded_file($fileTempName, $fileDestination);
                //Message after success
                echo "File Uploaded successfully";
                //Message,If file size greater than allowed size
                echo "File Size Limit beyond acceptance";
            //Message, If there is some error
            echo "Something Went Wrong Please try again!";
        //Message,If this is not a valid file type
        echo "You can't upload this extention of file";


Related Articles

Latest Articles