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

One common pitfall to avoid when using PHP for file uploads is not validating the file type before allowing it to be uploaded. This can lead to security vulnerabilities such as allowing malicious files to be executed on the server. To solve this issue, always validate the file type 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. Please upload a JPG, JPEG, PNG, or GIF file.";
    exit;
}

// Continue with file upload process