Introduction
PHP provides a simple and powerful way to handle file uploads through forms and server-side processing.
Understanding file upload in PHP is essential for building real-world applications like social media platforms, job portals, and content management systems.
What is File Upload in PHP?
File upload in PHP is the process of transferring a file from a user’s local system to a web server. This is typically done using an HTML form with a file input field and processed using PHP.
When a file is uploaded, PHP stores it temporarily on the server and provides information about the file using the $_FILES superglobal array.
- HTML Form with enctype=”multipart/form-data”
- <input type=”file”> field
- PHP script to handle the upload
Why it is used
File upload functionality is widely used in various applications:
1. Profile Image Upload
Users can upload profile pictures on websites.
2. Document Submission
Upload resumes, assignments, or PDFs.
3. Media Sharing
Upload images, videos, and audio files.
4. Data Storage
Store user-generated content on the server.
5. Backup Systems
Upload backup files or configurations.
Syntax
HTML Form for File Upload
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file:
<input type="file" name="myFile">
<input type="submit" value="Upload">
</form>
PHP Upload Script
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["myFile"]["name"]);
if (move_uploaded_file($_FILES["myFile"]["tmp_name"], $target_file)) {
echo "File uploaded successfully.";
} else {
echo "Error uploading file.";
}
$_FILES Array Structure
$_FILES['myFile']['name']; // File name
$_FILES['myFile']['type']; // File type
$_FILES['myFile']['size']; // File size
$_FILES['myFile']['tmp_name']; // Temporary file path
$_FILES['myFile']['error']; // Error code
Examples:
Example 1: Basic File Upload
if(isset($_POST['submit'])) {
$file_name = $_FILES['myFile']['name'];
$file_tmp = $_FILES['myFile']['tmp_name'];
move_uploaded_file($file_tmp, "uploads/" . $file_name);
echo "File uploaded successfully!";
}
Example 2: File Upload with Validation
$target_dir = "uploads/";
$file_name = $_FILES["myFile"]["name"];
$file_size = $_FILES["myFile"]["size"];
$file_tmp = $_FILES["myFile"]["tmp_name"];
$allowed_types = ["jpg", "png", "pdf"];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
if(in_array($file_ext, $allowed_types)) {
if($file_size < 2000000) { // 2MB limit
move_uploaded_file($file_tmp, $target_dir . $file_name);
echo "File uploaded successfully!";
} else {
echo "File is too large.";
}
} else {
echo "Invalid file type.";
}
Example 3: Rename File Before Upload
$file_name = $_FILES["myFile"]["name"];
$file_tmp = $_FILES["myFile"]["tmp_name"];
$new_name = time() . "_" . $file_name;
move_uploaded_file($file_tmp, "uploads/" . $new_name);
echo "File uploaded with new name!";
Real-Life Example
Example: Profile Picture Upload System
if(isset($_POST['upload'])) {
$file_name = $_FILES['profile']['name'];
$file_tmp = $_FILES['profile']['tmp_name'];
$target = "profile_images/" . $file_name;
if(move_uploaded_file($file_tmp, $target)) {
echo "Profile picture uploaded!";
} else {
echo "Upload failed.";
}
}
How it works:
- User selects an image
- PHP stores it temporarily
- File is moved to a permanent directory
- Image can be displayed later
Example: Resume Upload System
Used in job portals where users upload resumes:
$file_name = $_FILES['resume']['name'];
$file_tmp = $_FILES['resume']['tmp_name'];
move_uploaded_file($file_tmp, "resumes/" . $file_name);
echo "Resume uploaded successfully!";
Common Mistakes
1. Missing enctype Attribute
Without enctype=”multipart/form-data”, file upload will not work.
2. Not Validating File Type
Uploading any file type can lead to security issues.
3. Ignoring File Size Limit
Large files can crash the server.
4. Not Renaming Files
Uploading files with same name can overwrite existing files.
5. Not Checking Errors
Always check $_FILES[‘file’][‘error’].
6. Incorrect Folder Permissions
Upload folder must have write permission.
Conclusion
File upload in PHP is a powerful feature that enables users to send files from their local system to a web server. It is widely used in modern web applications such as social media, job portals, and content management systems.
By using $_FILES and functions like move_uploaded_file(), developers can easily implement file upload functionality. However, security should always be a top priority. Validating file types, limiting file sizes, renaming files, and handling errors properly are essential best practices.