What are some common pitfalls when using PHP for file uploads?

One common pitfall when using PHP for file uploads is not properly validating the file type before allowing it to be uploaded. This can lead to security vulnerabilities such as allowing malicious files to be uploaded to the server. To solve this issue, always validate the file type before moving it to the upload directory.

// Check if the file type is allowed before moving it to the upload directory
$allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif'];
$uploadedFileType = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if (!in_array($uploadedFileType, $allowedFileTypes)) {
    echo "Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.";
} else {
    // Move the file to the upload directory
    move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
    echo "File uploaded successfully.";
}