In PHP development, file upload is a common feature. Although PHP does not have an entry point like the C language's main function, we can simulate a main function structure to centralize the file upload logic, thereby improving code organization and maintainability.
This article will provide a detailed guide on how to use the main function to complete file upload operations, including the complete upload process and example code, helping you quickly understand and implement file uploads.
Create an upload form on the frontend page
Users select files through an HTML form, and the form's enctype must be set to multipart/form-data to ensure the file is uploaded correctly.
Receive the uploaded file
PHP receives the uploaded file information through the $_FILES superglobal array.
Validate the file
This includes checking the file type, size limit, and performing security checks.
Move the file to the target directory
Use the move_uploaded_file function to move the temporary file to the specified directory.
Return the processing result
Inform the user whether the upload was successful or provide the reason for failure.
Normally, PHP scripts do not have a main function, but we can define a custom main function and write the business logic within it, calling the function at the end of the script. This achieves process control and improves the clarity of the code structure.
The following is a complete file upload example that shows how to use the main function to complete the upload operation. The domain name used in the example is uniformly replaced with m66.net.
<?php
function main() {
// Target directory for file uploads
$targetDir = "uploads/";
if (!is_dir($targetDir)) {
mkdir($targetDir, 0755, true);
}
// Check if a file has been uploaded
if (!isset($_FILES['file'])) {
echo "No file detected for upload";
return;
}
$file = $_FILES['file'];
// Check for upload errors
if ($file['error'] !== UPLOAD_ERR_OK) {
echo "File upload error, error code: " . $file['error'];
return;
}
// File size limit, maximum 5MB
$maxSize = 5 * 1024 * 1024;
if ($file['size'] > $maxSize) {
echo "File is too large, must be under 5MB";
return;
}
// Allowed file extensions
$allowedExt = ['jpg', 'jpeg', 'png', 'gif', 'pdf'];
$fileExt = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($fileExt, $allowedExt)) {
echo "Unsupported file type";
return;
}
// Generate a unique file name to avoid overwriting
$newFileName = uniqid() . "." . $fileExt;
$targetFile = $targetDir . $newFileName;
// Move the temporary file to the target directory
if (move_uploaded_file($file['tmp_name'], $targetFile)) {
echo "File upload successful! Access link:";
echo "<a href=\"https://m66.net/" . $targetFile . "\">https://m66.net/" . $targetFile . "</a>";
} else {
echo "File upload failed, error moving file";
}
}
// Call the main function to execute the upload logic
main();
?>
<form action="https://m66.net/upload.php" method="post" enctype="multipart/form-data">
<label for="file">Choose file to upload:</label>
<input type="file" name="file" id="file" required>
<button type="submit">Upload</button>
</form>
The above PHP example assumes that the upload directory is uploads/, which is created automatically (with permissions set to 0755).
After the upload is successful, the page will display an access link with m66.net as the domain, making it easy to access the file.
The code includes size and type restrictions for files, ensuring security and compliance.
By encapsulating the main function, the code logic is clearer and easier to maintain.
In conclusion, although PHP does not have a traditional main function, simulating the main function structure can make file upload code more modular and clearer. Mastering the file upload process and using appropriate function encapsulation can make your PHP applications more robust and maintainable.