简体   繁体   English

zip中的php下载文件为空

[英]php downloaded files in zip are empty

currently I am trying to put files in a zip and download them. 目前,我正在尝试将文件放在zip文件中并下载。 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. 这将下载zip以及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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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