Table of contents
- File-upload related topics in PHP
File-upload related topics in PHP
Let's go through each step more comprehensively for the file upload example:
1. File Uploads using $_FILES
:
HTML Form (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload Form</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="userfile">Choose a file:</label>
<input type="file" name="userfile" id="userfile">
<input type="submit" value="Upload">
</form>
</body>
</html>
PHP File Upload Script (upload.php):
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["userfile"]["name"]);
$uploadOk = 1;
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, the file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["userfile"]["size"] > 500000) { // 500 KB
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow only certain file formats
$allowed_types = array("image/jpeg", "image/png", "image/gif");
$file_type = $_FILES["userfile"]["type"];
if (!in_array($file_type, $allowed_types)) {
echo "Invalid file type. Allowed types: " . implode(', ', $allowed_types);
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $target_file)) {
echo "The file ". basename($_FILES["userfile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
2. Handling File Upload Errors:
Include the following code snippet at the beginning of the upload.php
script to handle file upload errors:
if ($_FILES["userfile"]["error"] > 0) {
echo "Error: " . $_FILES["userfile"]["error"];
// Handle specific errors if needed
// 1 - UPLOAD_ERR_INI_SIZE: The uploaded file exceeds the upload_max_filesize directive in php.ini
// 2 - UPLOAD_ERR_FORM_SIZE: The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form
// 3 - UPLOAD_ERR_PARTIAL: The uploaded file was only partially uploaded
// 4 - UPLOAD_ERR_NO_FILE: No file was uploaded
// 6 - UPLOAD_ERR_NO_TMP_DIR: Missing a temporary folder
// 7 - UPLOAD_ERR_CANT_WRITE: Failed to write file to disk
// 8 - UPLOAD_ERR_EXTENSION: A PHP extension stopped the file upload
exit;
}
3. File Types and MIME Checking:
Include the following code snippet after checking file size in the upload.php
script:
$allowed_types = array("image/jpeg", "image/png", "image/gif");
$file_type = $_FILES["userfile"]["type"];
if (!in_array($file_type, $allowed_types)) {
echo "Invalid file type. Allowed types: " . implode(', ', $allowed_types);
$uploadOk = 0;
}
4. File Size Limitations:
Include the following code snippet after checking if the file already exists in the upload.php
script:
$max_size = 500000; // 500 KB
if ($_FILES["userfile"]["size"] > $max_size) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
5. Uploading to a Specific Directory:
The example already includes code to upload files to a specific directory (uploads/
).
6. File Renaming:
Include the following code snippet after defining $target_file
in the upload.php
script:
$new_filename = "custom_name.jpg";
$target_file = $target_dir . $new_filename;
7. Security Considerations:
Implement additional security measures based on your application's requirements, such as validating file types, checking for malicious content, and setting appropriate permissions on upload directories.
8. Multiple File Uploads:
Modify the HTML form to allow multiple file uploads:
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="userfile">Choose files:</label>
<input type="file" name="userfile[]" id="userfile" multiple>
<input type="submit" value="Upload">
</form>
Modify the PHP script to handle multiple uploads:
// Handle multiple uploads
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
$name = basename($_FILES["userfile"]["name"][$key]);
move_uploaded_file($tmp_name, "uploads/" . $name);
}
}
9. File Downloading:
Create a download.php file with the following code:
<?php
$file_path = "path/to/file.txt";
header("Content-disposition: attachment; filename=" . basename($file_path));
header("Content-type: text/plain");
readfile($file_path);
?>
10. File Deletion:
Create a delete.php file with the following code:
<?php
$file_to_delete = "path/to/file.txt";
if (file_exists($file_to_delete)) {
unlink($file_to_delete);
echo "File deleted successfully.";
} else {
echo "File not found.";
}
?>
11. File Reading and Writing:
Create a log.php file with the following code:
<?php
// Writing to a file
$file_path = "path/to/log.txt";
$data_to_write = "Log entry: " . date("Y-m-d H:i:s") . "\n";
file_put_contents($file_path, $data_to_write, FILE_APPEND | LOCK_EX);
// Reading from a file
$file_content = file_get_contents($file_path);
echo $file_content;
?>
These examples cover a range of file-related operations in PHP. Make sure to customize them based on your specific needs and enhance security measures as necessary.