Quartermain
Quartermain

Reputation: 173

How do I exclude files from zip download?

Currently I use this code to download a zip file:

<?php
    $weborder = $_POST['weborder'];
    $printlocation = $_POST['print'];

$file = 'z:\Backup\\'.$printlocation.'\\'.$weborder.'.zip';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
else
{
    echo ":(";
}
?>

The zip file contains 4 files. The problem that I am having right now is that 3 files shouldnt be downloaded. I was wondering if there is a simple way to select which files dont get downloaded.

Upvotes: 0

Views: 307

Answers (1)

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

Unfortunately there is no way for selective zip download. You should repack you zip archive with files you want to serve for download.


Here are few related question just to not duplicate them here:

What you need to do is to extract files you want to serve for downloading. You can accomplish this by using "selective" unzip ...

$zip->extractTo('/some/dir/', ['number_one.file', 'some_other.file']); // or single file in your case

... and then pack them, and serve newly created zip archive with files you wanted.

More info on how to do this you can find in related links above.


UPD: And I would mention that it is not a good practice to use user input directly in your scripts. It is potential security breach.

Upvotes: 1

Related Questions