What is a common method for limiting file size in PHP uploads and what potential issue arises from using $_FILES['file']['size']?
One common method for limiting file size in PHP uploads is by using the $_FILES['file']['size'] variable to check the size of the uploaded file. However, the potential issue that arises from using this method is that the value of $_FILES['file']['size'] is controlled by the client-side and can be easily manipulated. To solve this issue, it is recommended to perform server-side validation by checking the file size after the upload is complete and before processing the file.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$maxFileSize = 5 * 1024 * 1024; // 5 MB
$uploadedFileSize = $_FILES['file']['size'];
if ($uploadedFileSize > $maxFileSize) {
echo 'Error: File size exceeds the limit of 5 MB.';
} else {
// Process the uploaded file
}
}