PHP File Upload Tutorial
markdown
Uploading files is one of the most common features in modern web applications. Whether you're allowing users to upload profile pictures, documents, CSV files, or any media, PHP provides simple and secure ways to handle file uploads.
This tutorial covers:
How file uploads work in PHP
Creating an HTML upload form
Handling file uploads with PHP
Validating file size and type
Uploading multiple files
Best practices and security tips
PHP handles file uploads using the $_FILES superglobal array. It contains information about uploaded files such as:
file name
file type
file size
temporary file location
upload status
To enable file uploads, your HTML form must include:
method="POST"
enctype="multipart/form-data"
<!DOCTYPE html>
<html>
<head>
<title>PHP File Upload</title>
</head>
<body>
<h2>Upload File</h2>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<label>Select File:</label><br><br>
<input type="file" name="myfile" required><br><br>
<button type="submit">Upload</button>
</form>
</body>
</html>
<?php
if (isset($_FILES['myfile'])) {
$file_name = $_FILES['myfile']['name'];
$file_tmp = $_FILES['myfile']['tmp_name'];
$file_size = $_FILES['myfile']['size'];
$file_error = $_FILES['myfile']['error'];
if ($file_error === 0) {
$destination = "uploads/" . $file_name;
if (move_uploaded_file($file_tmp, $destination)) {
echo "File uploaded successfully!";
} else {
echo "Error uploading file.";
}
} else {
echo "Upload error!";
}
}
?>
Make sure you create an uploads/ folder in your project.
$allowed_types = ["jpg", "png", "jpeg", "gif"];
$max_size = 2 * 1024 * 1024; // 2MB
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
if (!in_array($file_ext, $allowed_types)) {
echo "Invalid file type! Only images allowed.";
exit;
}
if ($file_size > $max_size) {
echo "File is too large! Maximum allowed size is 2MB.";
exit;
}
Avoid name conflicts by renaming files.
$new_name = uniqid("file_", true) . "." . $file_ext;
$destination = "uploads/" . $new_name;
<form action="multi-upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<button type="submit">Upload Files</button>
</form>
<?php
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$name = $_FILES['files']['name'][$key];
$tmp = $_FILES['files']['tmp_name'][$key];
move_uploaded_file($tmp, "uploads/" . $name);
}
echo "Multiple files uploaded successfully!";
?>
if ($file_error !== UPLOAD_ERR_OK) {
echo "File upload error: " . $file_error;
}
| Error Code | Meaning |
|---|---|
0 | No error |
1 | File exceeds upload_max_filesize |
2 | File exceeds form MAX_FILE_SIZE |
3 | File partially uploaded |
4 | No file uploaded |
Always validate file extensions
Restrict file types to images or safe formats
Rename uploaded files
Store uploads outside the public root if possible
Never trust MIME type alone
Use mime_content_type() for verification
Limit file size in php.ini
$ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$new_name = "profile_" . time() . "." . $ext;
move_uploaded_file($file_tmp, "uploads/profile/" . $new_name);
echo "Profile image uploaded successfully!";
File uploading in PHP is simple and powerful when handled correctly. By validating file types, restricting sizes, and securing upload directories, you can safely allow users to upload images, documents, and more.
This tutorial gives you all the core concepts needed to build secure and efficient file upload systems.
Learn PHP file upload with step-by-step examples. Includes single and multiple file uploads, validation, security, and practical usage.
php file upload tutorial, php upload example, php move_uploaded_file, php multiple upload, php file handling
"PHP file upload tutorial example for beginners"