What are the potential pitfalls of using the move_uploaded_file function in PHP?
One potential pitfall of using the move_uploaded_file function in PHP is that it does not perform any validation on the uploaded file before moving it to the destination directory. This can lead to security vulnerabilities such as allowing malicious files to be uploaded and executed on the server. To mitigate this risk, it is important to validate the file before moving it using functions like is_uploaded_file and getimagesize.
// Validate the uploaded file before moving it
if (is_uploaded_file($_FILES['file']['tmp_name']) && getimagesize($_FILES['file']['tmp_name'])) {
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
echo "File is valid, and was successfully uploaded.";
} else {
echo "Possible file upload attack!";
}
} else {
echo "Invalid file.";
}