What are some common pitfalls to avoid when uploading and unpacking zip files using PHP?

One common pitfall to avoid when uploading and unpacking zip files using PHP is not checking for file extensions or file types before processing the uploaded file. This can lead to security vulnerabilities such as allowing malicious scripts to be executed on the server. To prevent this, always validate the file extension and MIME type before extracting the contents of a zip file.

// Check if the uploaded file is a zip file
if ($_FILES['zip_file']['type'] != 'application/zip') {
    die('Only zip files are allowed.');
}

// Check if the file has a .zip extension
$ext = pathinfo($_FILES['zip_file']['name'], PATHINFO_EXTENSION);
if ($ext != 'zip') {
    die('Invalid file extension. Only zip files are allowed.');
}

// Unpack the zip file
$zip = new ZipArchive;
$res = $zip->open($_FILES['zip_file']['tmp_name']);
if ($res === TRUE) {
    $zip->extractTo('/path/to/extract/');
    $zip->close();
    echo 'Zip file extracted successfully.';
} else {
    echo 'Failed to extract zip file.';
}