What are some potential pitfalls to watch out for when developing PHP scripts for file uploads?
One potential pitfall when developing PHP scripts for file uploads is not properly validating the uploaded file. This can lead to security vulnerabilities such as allowing malicious files to be uploaded to the server. To mitigate this risk, always validate the file type, size, and content before allowing the upload to proceed.
// Validate the uploaded file before moving it to the server
$allowedTypes = ['image/jpeg', 'image/png'];
$maxSize = 5242880; // 5MB
if (!in_array($_FILES['file']['type'], $allowedTypes) || $_FILES['file']['size'] > $maxSize) {
echo 'Invalid file type or size.';
exit;
}
// Move the uploaded file to a secure location
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);