PHP 7 File Upload | PHP Tutorial

PHP 7 File Upload | PHP Tutorial

PHP file upload. This tutorial explains, how you can easily upload files on a remote server using HTML 5 and PHP 7. We will also learn how to validate file types on the server-side and show an error message.

When we work with PHP, we need to upload several types of files like images, zip files, PDF files, document files, text files, video files, audios files on a remote web server.

Download Source Code

PHP File Upload Tutorial

You will follow the below steps and learn how you can easily upload files on a remote server with validation using PHP.

  • First, create one file name index.php
  • Second, create uplaod.php file

First, create one file name index.php

Now we need to create an index.php file and update the below HTML code into your index.php file. The following HTML code shows the HTML form, where you can upload the files on the webserver.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP File Upload Example - Tutsmake.com</title>
</head>
<body>
	<form action="upload.php" method="post" enctype="multipart/form-data">
	    <h2>PHP Upload File</h2>
	    <label for="file_name">Filename:</label>
	    <input type="file" name="anyfile" id="anyfile">
	    <input type="submit" name="submit" value="Upload">
	    <p><strong>Note:</strong> Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 5 MB.</p>
	</form>
</body>
</html>

Second, create uplaod.php file

Now we need to create one file name upload.php file and update the below code into your upload.php file. The following PHP code uploads the files from the web server with validation. Here also we will validate the size of the file.

<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Check if file was uploaded without errors
    if(isset($_FILES["anyfile"]) &amp;&amp; $_FILES["anyfile"]["error"] == 0){
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        $filename = $_FILES["anyfile"]["name"];
        $filetype = $_FILES["anyfile"]["type"];
        $filesize = $_FILES["anyfile"]["size"];
    
        // Validate file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
        // Validate file size - 10MB maximum
        $maxsize = 10 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
        // Validate type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("upload/" . $filename)){
                echo $filename . " is already exists.";
            } else{
                if(move_uploaded_file($_FILES["anyfile"]["tmp_name"], "upload/" . $filename)){

                    echo "Your file was uploaded successfully.";
                }else{

                   echo "File is not uploaded";
                }
                
            } 
        } else{
            echo "Error: There was a problem uploading your file. Please try again."; 
        }
    } else{
        echo "Error: " . $_FILES["anyfile"]["error"];
    }
}
?>

Know About – Upload.php File Code

Here, if any you uploaded a file using this field name anyfile, we can obtains its details like the name, type, size, temporary name or any error occurred while attempting the upload via the $_FILES[“anyfile”] associative array, like this:

1). $_FILES[“anyfile”][“name”] — This array value specifies the original name of the file, including the file extension. It doesn’t include the file path.

2). $_FILES[“anyfile”][“type”] — This array value specifies the MIME type of the file.

3). $_FILES[“anyfile”][“size”] — This array value specifies the file size, in bytes.

4). $_FILES[“anyfile”][“tmp_name”] — This array value specifies the temporary name including full path that is assigned to the file once it has been uploaded to the server.

5). $_FILES[“anyfile”][“error”] — This array value specifies error or status code associated with the file upload, e.g. it will be 0, if there is no error.

Conclusion

In this PHP file upload tutorial, you have learned how you can easily upload files on web server using PHP. You also learned how you can validate the size and type of files.

Download Source Code

Leave a Reply

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