A Brief Description of move_uploaded_file() Function in PHP

This article presents A Brief Description of move_uploaded_file() Function in PHP.

For the purpose of uploading a file, we use the move_uploaded(_file() function. Basically, it takes two parameters as the input. While the first parameter indicates the name of the uploaded file. Similarly, the second parameter indicates the location to which the file needs to be moved. So, both of these parameters are string-type parameters.

Additionally, this function also checks the validity of the existing file. So, it moves the file only if it is a valid one. Also, it performs checks on other sources of errors and determines whether the file can be moved or not. When there are no errors, the move_uploaded_file() function moves the file to the desired destination.

Example of move_uploaded_file() Function in PHP

At first, you need to create a form that allows users to browse a file. Further, add an Upload button also. You can refer to my previous article for the code.

Example of move_uploaded_file() Function in PHP - HTML Form
Example of move_uploaded_file() Function in PHP – HTML Form

After that, you need to write the PHP script that actually moves the file to the specified location on the server. The following code demonstrates how to use move_uploaded_file() Function in PHP.

   <title>File Upload Example</title>
   <form name="myform" method="POST" action="fileupload1.php" enctype="multipart/form-data">
     Select a File: <input type="file" name="t1"/><br><br>
      <input type="submit" name="submit" value="Upload"/>
     echo 'Invalid Directory!';

At first, we need to check whether the user has clicked on the submit button or not. It prevents the PHP script to execute as the page loads initially. When user selects the file and clicks on the submit button, the first if statement evaluates to true. Further, it checks whether the specified directory exists or not. After that, the move_uploaded_file() function executes and uploads the file in the given directory.

However, still there is need to apply further checks. For instance, we can check only certain file types should upload. Also, we can check the file size before uploading. The complete example of uploading files is given here.

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

You may also like...


  1. Nice Post

  2. Nice post

  3. Thanks.
    Please try to publish some tutorials about django framework.

Leave a Reply

Your email address will not be published. Required fields are marked *