PHP File Upload
Sure! Let's go through the basics of PHP file upload with an example.
Step-by-Step Guide to PHP File Upload
-
HTML Form for File Upload Create an HTML form that allows users to select a file to upload.
<!DOCTYPE html> <html> <body> <h2>File Upload Form</h2> <form action="upload.php" method="post" enctype="multipart/form-data"> Select file to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload File" name="submit"> </form> </body> </html>
This form uses the
POST
method and theenctype="multipart/form-data"
attribute, which is necessary for file uploads. -
PHP Script to Handle File Upload Create a PHP script (
upload.php
) that handles the file upload process.<?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?>
This script performs several checks:
- Checks if the uploaded file is an actual image.
- Checks if the file already exists in the
uploads/
directory. - Checks if the file size is within the limit (500 KB in this case).
- Checks if the file type is allowed (JPG, JPEG, PNG, GIF).
- If all checks pass, the file is moved to the
uploads/
directory.
Explanation of PHP Code
-
Directory and File Path Setup:
$target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
-
Check if Image File:
if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } }
-
Check if File Already Exists:
if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; }
-
Check File Size:
if ($_FILES["fileToUpload"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0; }
-
Allow Certain File Formats:
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; }
-
Upload File if No Errors:
if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } }
Output
When you run the HTML form and upload a file, you will see messages based on the checks performed:
-
If the file is not an image:
File is not an image.
-
If the file already exists:
Sorry, file already exists.
-
If the file is too large:
Sorry, your file is too large.
-
If the file format is not allowed:
Sorry, only JPG, JPEG, PNG & GIF files are allowed.
-
If there is an error uploading the file:
Sorry, there was an error uploading your file.
-
If the file is successfully uploaded:
The file filename.jpg has been uploaded.
This is a basic example. In a real-world scenario, you might want to add more security checks and handle other possible errors.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.