0

My code creates a ZIP file and downloads it in this ZIP folder some images exist.

It’s done well but problem is that after I download and extract this folder then all path folder also become my code is:

$id         = $row['id'];
$doc_name   = $row['doc_name'];
$file_names = explode(',',$doc_name);
$file_path  = $_ROOT_REQUIRE."uploads/document/";
$archive_file_name = "demo.zip";
$zip        = new ZipArchive($archive_file_name);
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE ) !== TRUE) {
    echo "cannot open <$archive_file_name>\n";
    exit("cannot open <$archive_file_name>\n");
}
foreach ($file_names as $files) {
    if ($files !== '') {
        $zip->addFile("$files", basename($files));
    }
} 
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($archive_file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($archive_file_name));
header('Cache-Control: private');
ob_clean();
flush();
readfile(basename($archive_file_name));
exit;
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
wild
  • 340
  • 1
  • 3
  • 14

3 Answers3

1

Your Code look's is Fine. Just Replace Your Foreach Loop .

foreach ($files as $file)
{
    $file = str_replace('\\', '/', $file);          
    if(filetype($file) == 'file') {         
        $zip->addFile( $file, pathinfo( $file, PATHINFO_BASENAME ) );
    }
}

it should be work Thanks

Ravi D
  • 83
  • 7
0

A bit unclear on why this would be happening, looking at this line:

$zip->addFile("$files", basename($files));

Why are there " quotes around $files? I would recommend changing that to:

$zip->addFile($files, basename($files));
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

These are the changes I made and it work well now. Thanks to all.

  1. $overwrite = true;

  2. if($zip->open($archive_file_name,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true)

  3. This larger piece:

    foreach ($file_names as $file) {
        $files = $file_path.$file;
        $files = str_replace('\\', '/', $files);          
        if(filetype($files) == 'file') {
            $zip->addFile( $files, pathinfo( $files, PATHINFO_BASENAME ) );
        }
    }  
    
Till Helge
  • 9,253
  • 2
  • 40
  • 56
wild
  • 340
  • 1
  • 3
  • 14