[英]Downloaded zip archive empty after adding csv file
The Problem: 问题:
When I download a zip archive that is supposed to have a csv file in it, I get an empty archive or invalid archive. 当我下载应该包含csv文件的zip存档时,我得到了一个空的存档或无效的存档。 Now, I can make the zip archive with the csv in it, on the server and download via FileZilla. 现在,我可以在服务器上制作包含csv的zip存档,并通过FileZilla下载。 This is not the desired use case. 这不是所需的用例。 User wants to download automatically after clicking a button. 用户想要在单击按钮后自动下载。
About program: 关于程序:
Agents sign up for event (ie RSVP to it) and provide a set of photos. 代理注册事件(即对事件进行RSVP)并提供一组照片。 For every agent, there are +1 photos. 每位特工都有+1张照片。 User wants to download a list of all RSVPs for each event (ie the sign up data). 用户想要为每个事件下载所有RSVP的列表(即注册数据)。 For each RSVP, User also wants to see the photos they uploaded to the server. 对于每个RSVP,用户还希望查看他们上传到服务器的照片。
Goal of program: 计划目标:
Current Status of Program: 计划的现状:
CSV file created. CSV文件已创建。 Below it is added to ZipArchive object. 在其下方将添加到ZipArchive对象。 Downloaded zip file and found it empty . 下载了zip文件,发现它为空 。
//store the result of the query. Included to show process
$result = mysqli_query($dbh, $sql);
//open the file to write the information to.
$file = fopen('event-report.csv', 'w');
$txt .= 'Title Company,' .'Event ID,' . 'Event Time,' . 'First Name,' . 'Last Name,' . 'Email,' . 'Agent Company,' . 'Phone Number,' . 'Listing ID,' . 'URL,' . 'Address,' . 'ZIP,' . "\n";
while ($row = mysqli_fetch_row($result)){
for ($i=0; $i<12; $i++){
$txt .= $row[$i] . ','; //About the agent attending
}
$txt .= "\n";
$listings[$listing_count] = $row[8];
}
fputs($file, $txt); //store into csv file
fclose($file); //verified on server; contains desired output
$zip = new ZipArchive();
$dir = 'tmp'; //create temporary directory with write access
if ( !file_exists($dir) ) {
mkdir ($dir, 0755); //Everything for owner, read/execute for others
}
if($zip->open('test.zip', ZIPARCHIVE::CREATE) === TRUE ){
$zip->addFile('event-report.csv');
if (file_exists($zip->getFromName('event-report.csv'))){
echo "Exists";
}
else{
echo "Does Not Exist";
echo $_SERVER['DOCUMENT_ROOT'];
}
}
else {
echo "Failed code";
}
$zip->close();
if(!$return){
echo " You do not have write access to current folder.";
}
else{
echo "You do have access."; //without headers, the zip containing csv successfully created on server.
}
header('Content-disposition: attachment; filename=download.zip');
header('Content-type: application/zip');
readfile($dir.'/test.zip'); //When downloaded: still empty/invalid.
Possible Problem(s) 可能的问题
$zip->addFile('/f1/f2/f3/f.csv');
Questions 问题
添加到归档文件时,您必须使用绝对路径来明确显示。
$zip->addFile($abs_path, $relative_path);
Solution: flush headers before calling readfile(). 解决方案:在调用readfile()之前刷新标头。 I learned that I do not need flush() but I left it in there anyways. 我了解到我不需要flush(),但无论如何我还是把它留在那里。 See SO post that helped me include these two statements here 看到这样的帖子,它帮助我在此处包括了这两个声明
header('Content-type: application/zip');
header('Content-disposition: attachment; filename=test.zip');
ob_clean(); // discard any data in the output buffer (if possible)
flush(); // flush headers (if possible)
readfile('tmp/test.zip');
exit();
I also changed some minor things with the error checking that I thought I would mention below: 我还通过错误检查更改了一些小事情,我认为我会在下面提到:
I made a directory with certain permissions that will hold the files I want to put into the archive. 我创建了一个具有某些权限的目录,该目录将保存我要放入存档的文件。 This is done at the beginning before the csv and zip are created. 这是在创建csv和zip的开始时完成的。
$dir = 'tmp/';
if ( !file_exists($dir) ) {
mkdir ($dir, 0777);
}
Opening the file has changed too. 打开文件也已更改。 It now involves the new directory I make: 现在,它涉及到我创建的新目录:
$zip = new ZipArchive;
if($zip->open($dir.'test.zip', ZIPARCHIVE::CREATE) === TRUE ){
$res = $zip->addFile('tmp/event-report.csv', 'event-report.csv'); //add the report csv file.
if ($res){
echo "Exists ";
}
else{
echo "Does Not Exist ";
//echo $_SERVER['DOCUMENT_ROOT'];
}
}
Thanks to everyone that contributed or just viewed this question. 感谢所有贡献者或刚刚看过这个问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.