PHP File Upload

In this tutorial, we will learn how to upload files using PHP.

We can easily upload files to the server using PHP & HTML Form. PHP has the ability to upload different kind of files. We can upload images, audios, videos, ZIP files, MS Office documents, PDFs and many others.

We need to be careful when uploading file. We can check file type & size, so that we allow only valid file to upload. This will be shown in later example.


Important Things about File Upload:

  • We need to use <input type="file"> in HTML form to create file upload button.
  • File upload requires that HTML form method is set to “post”
  • The form also needs attribute: enctype="multipart/form-data"
  • After upload, all file related information is available in $_FILES variable

PHP $_FILES variable:

$_FILES is a PHP global variable (actually array) that we use during file uploading. This variable contains all the information such as file name, file type, file size, temp file location and errors related to file that we are uploading.

Let us assume that we are using <input type="file" name="user_file" > to create upload field. Then $_FILES variable will have information as mentioned in following table.

Global $_FILES VariableDescription
$_FILES[‘user_file’][‘name’]returns file name of uploaded file.
$_FILES[‘user_file’][‘type’]returns MIME type/extension of the file.
$_FILES[‘user_file’][‘size’]returns size of the file (in bytes).
$_FILES[‘user_file’][‘tmp_name’]returns temporary file name along with location on server.
$_FILES[‘user_file’][‘error’]returns error code related to file.

PHP move_uploaded_file() function:

After form submission, file gets uploaded to temporary folder first. Then, we can use a PHP function to move that file from temporary location to desired folder.

The move_uploaded_file() function is used to move the uploaded file to new location. This function returns TRUE on success & FALSE if any error occurs. Therefore, we will use return value to display success or failure message accordingly.

Syntax:
move_uploaded_file ( $file_name , $destination )


PHP File Upload – Simple Example

<html>
    <body>
        <form action="file2.php" method="post" enctype="multipart/form-data">
        Upload Your File: <input type="file" name="user_file">
        <input type="submit">
        </form>
    </body>
</html>
<?php
$file_name = $_FILES['user_file']['name'];
$file_source_location = $_FILES['user_file']['tmp_name'];
$file_size = $_FILES['user_file']['size'];
$file_target_location = "upload/" . $file_name;
 
$file_upload_status = move_uploaded_file($file_source_location, $file_target_location);
 
if ($file_upload_status == true) {
    echo "Congratulations. File Uploaded to: $file_target_location";
} else {
    echo "Sorry. File uploading failed!";
    print_r(error_get_last());
}
?>

PHP File Upload – Example with file size & file type check

When we allow users to upload file, user can upload any kind of file. This can be dangerous because some type of files can hack your website & destroy your data. Another problem is that sometimes user upload large size files that can exceed your web hosting storage space.

As mentioned in the table, PHP $_FILES variable provides different type of information regarding file. This information help us to check file size & type before uploading to server. In the following example, we will restrict users to upload image file (png & jpg) less than 1 mb.

<html>
    <body>
        <form action="file2.php" method="post" enctype="multipart/form-data">
        User Name: <input type="text" name="user_name" /><br>
        Upload File: <input type="file" name="user_file" />
        <input type="submit">
        </form>
    </body>
</html>
<?php
$file_name = $_FILES['user_file']['name'];
$file_source_location = $_FILES['user_file']['tmp_name'];
$file_size = $_FILES['user_file']['size'];
$file_target_location = "upload/" . $file_name;
$file_type = pathinfo($file_name, PATHINFO_EXTENSION);
 
if ($file_type != "jpg" && $file_type != "png") {
    echo "Sorry. Only image files .jpg and .png is allowed to upload";
    die();
}
if ($file_size > 1048576) {
    echo "Sorry. Your file is too large. You can upload less than 1 Mb";
    die();
}
 
$file_upload_status = move_uploaded_file($file_source_location, $file_target_location);
 
if ($file_upload_status == true) {
    echo "Congratulations. File Uploaded to: $file_target_location";
} else {
    echo "Sorry. File uploading failed!";
    print_r(error_get_last());
}
?>

Common Errors during File upload:

Warning: failed to open stream: No such file or directory…”
Solution: This error occurs if a target file/folder location is not valid. In the above example, if the “upload” folder does not exist, it will throw an error. Simply create an “upload” folder where you are running PHP Upload script.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.