What are common issues when trying to process file uploads directly in PHP?
Common issues when processing file uploads directly in PHP include insufficient file upload size limits, lack of proper file type validation, and potential security vulnerabilities if not handled correctly. To solve these issues, make sure to set appropriate upload_max_filesize and post_max_size values in php.ini, validate file types using the $_FILES['file']['type'] parameter, and move uploaded files to a secure directory with restricted permissions.
// Set maximum file upload size limit
ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '10M');
// Validate file type
$allowedFileTypes = ['image/jpeg', 'image/png'];
if (!in_array($_FILES['file']['type'], $allowedFileTypes)) {
die('Invalid file type. Allowed types: jpeg, png');
}
// Move uploaded file to secure directory
$targetDir = 'uploads/';
$targetFile = $targetDir . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $targetFile);