What are the common mistakes to avoid when creating an upload system in PHP?

One common mistake to avoid when creating an upload system in PHP is not checking 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 allowing the upload to proceed.

// Check file type before allowing upload
$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.";
    exit;
}

// Proceed with file upload
// Your upload code here