php下载的zip文件为空
目前,我正在尝试将文件放在zip中并下载它们。我使用以下代码:php下载的zip文件为空,php,file,zip,Php,File,Zip,目前,我正在尝试将文件放在zip中并下载它们。我使用以下代码: # 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){
# 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);
通过以下方式将文件添加到阵列中:
$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;
}
}
}
$weborder=$\u POST['weborder'];
$printlocation=$_POST['print'];
$dir=“z:\Backup\\$printlocation\\$weborder.zip”;
$zip=新的ZipArchive;
$files=array();
如果($zip->open($dir))
{
对于($i=0;$i<$zip->numFiles;$i++)
{
如果($zip->getNameIndex($i)!=“order info.txt”&&&$zip->getNameIndex($i)!=“workrequest.xml”&&&$zip->getNameIndex($i)!=“workrequest.pdf”)
{
$filename=$zip->getNameIndex($i);
$files[$i]=$dir.\\'$filename;
}
}
}
这将下载zip和zip中的文件。我遇到的唯一问题是文件是空的。此代码正在工作。请确保您的文件是否存在
$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 ." 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; }
而不是
$zip->addFromString(basename($file),$download_file);
试一试
文件是否添加到zip?是的。我添加了用于将文件添加到zip的方法。我尝试了此方法,但出现以下错误:“没有这样的文件或目录”和“函数名必须是字符串”我正在提供正确的文件路径。仅当文件大小为0kb时才下载文件。请按以下方式替换文件头->文件头('Content-disposition:attachment;filename='。$tmp_file);标题('Content-Length:'.filesize($tmp_file));标题(“内容类型:应用程序/zip”);不幸的是,这也没有帮助。我试图下载的文件已经在一个zip文件中。当我尝试你的代码时,它说文件不存在。是否可以在已经位于zip中的zip中下载文件?请尝试下面的代码下载您的zip文件“下载现有文件”使用此代码
$zip->addFromString(basename($file),$download_file);
$zip->addFile($basename($file));