简体   繁体   中英

php downloaded files in zip are empty

currently I am trying to put files in a zip and download them. I use the following code:

 # create new zip opbject
$zip = new ZipArchive();

# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);

# loop through each file
foreach($files as $file){

    # download file
    $download_file = file_get_contents($file);

    #add it to the zip
    $zip->addFromString(basename($file),$download_file);

}

# close zip
$zip->close();

# send the file to the browser as a download
header('Content-disposition: attachment; filename=Resumes.zip');
header('Content-type: application/zip');
readfile($tmp_file);

The files are added to the array the following way:

$weborder = $_POST['weborder'];
$printlocation = $_POST['print'];

$dir = "z:\Backup\\$printlocation\\$weborder.zip";

$zip = new ZipArchive; 
$files = array();

if ($zip->open($dir)) 
{
    for($i = 0; $i < $zip->numFiles; $i++) 
    {
        if ($zip->getNameIndex($i) != "order-info.txt" && $zip->getNameIndex($i) != "workrequest.xml" && $zip->getNameIndex($i) != "workrequest.pdf") 
        {
            $filename = $zip->getNameIndex($i);

            $files[$i] = $dir . "\\" . $filename;
        }
    }
}

This downloads the zip and the files that are in the zip. The only problem I am having is that the files are empty.

This code is working make sure that your files are existed or not.

$array = array("sites/README.txt","sites/chessboard.jpg"); //files to Add/Create in zip file

$zip = new ZipArchive(); // Load zip library 
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{ 
 // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($array as $key => $value)
{ 
    if(file_exists($value)){
        $zip->addFile($value); // Adding files into zip
    }else{echo $value ."&nbsp;&nbsp;file not exist<br/>";}
}
$zip->close();
if(file_exists($zip_name))
{
    echo "yes";die;
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}else{echo "zip not created";die; }

For Download Existing file

 $zip_name = "YOUR_ZIP_FILE PATH";
    if(file_exists($zip_name))
    {
    // push to download the zip
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
    readfile($zip_name);
    // remove zip file is exists in temp path
    //unlink($zip_name);
    }else{
        echo "zip not created";exit; 
    }

instead of

 $zip->addFromString(basename($file),$download_file);

try

$zip->addFile($basename($file));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM