Can skip About and Goal if necessary. They provide context/background about program.
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. Now, I can make the zip archive with the csv in it, on the server and download via FileZilla. This is not the desired use case. User wants to download automatically after clicking a button.
About program:
Agents sign up for event (i.e. RSVP to it) and provide a set of photos.
For every agent, there are +1 photos. User wants to download a list of all RSVPs for each event (i.e. the sign up data). For each RSVP, User also wants to see the photos they uploaded to the server.
Goal of program:
- Create csv file on server of mysql query (each row is an RSVP)
- For each row, create zip archive for that row's/RSVP's photos.
- Wanted: 1 csv file and multiple zip archives all in one big archive
- End result: Add all files into one zip archive and download to User.
- Addendum: This big zip file needs to be freshly generated every time it is requested.
Current Status of Program:
CSV file created. Below it is added to ZipArchive object. Downloaded zip file and found it empty.
//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 is not finding the file because it is being created at the same time.
1a. Solved:. Files created can be put into zip archive right after closing. - addFile parameter(s) are incorrect or lacking.
2a. Man page says for this parameter: "path to the file to add"
2b. Tried putting the path after the domain name but no success:
$zip->addFile('/f1/f2/f3/f.csv');
2c. PHP Manual > ZipArchive::addFile- i. Solved: The parameter is just the name of the file. No path is needed in this case.
Questions
- How do I add a csv file, a zip archive, or any file in general to another zip archive so that it will not be empty when I download it?
- Is this implementation incorrect because I am trying to add a file to a zip archive right after creating it? See Possible Problem(s) 1.
- If this question can be solved by a little reading, then what resources can you link that can guide me along? I have spent an
entire day reading in regards to this issue and do not feel like I
understand what I am missing here.
3a. file permissions