I have the following code running on my site. The only problem I have with it is that it makes a zip file on the server and then user downloads.
I would like to know what should I do in order for the zip file to be generated "on the fly" without being dumped on the server disk first. I would also like to make possible for the user to pause/resume the download.
//function for zip
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
//create the object
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.str_replace('./','',$files),translit($files).".mp3");
}
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
There are three requirements you mention:
zip is available for download on fly - I presume that by this, you mean "zip file is created on the fly". This is already happening. Indeed, it is what your script does, it's raison d'etre, if you will.
zip file should not be created on server - You have to create a file on the server, even if it's only temporarily, because that is how the Zip extension works. You can delete it after the user has downloaded it (just add unlink($archive_file_name); on the line before exit;).
user can also resume it if paused - This requirement is (mostly) incompatible with zip file should not be created on server. Resumable downloads are implementable in PHP, but it is quite difficult to do and requires access to the Range: header of the request - which not every server will allow you to have. Also, you would have to generate the whole file for even a partial request, because you have deleted it from the server. Apache has an implementation of resumable downloads, but it requires (AFAIK) that the file be static on the hard drive, and requested directly. This would mean that deleting the file after it was downloaded (at the end of the PHP script) would break the resumability.
Reading between the lines, I suspect the problem you are having is that your server's hard drive space is getting used up by all the Zip archives you are creating and not deleting. The solution to this (while still allowing resumable downloads) is to implement some form of TTL checker on the server and periodically deleting files that are older than, for example, 1 day. You could do this with a cron job, or by running the check when you go to create a new arhive.
At the moment, your code does not specify where the zip files will be created, and this is something you would need to do. Here is an example that assumes your script is in the root directory of your site, and that there is a directory called zips in the root directory of your site.
The basic flow is:
Loop through the /zips directory, and delete all files that are older than 1 day.
Create a new archive in the /zips directory
Redirect the user to the path of that static file.
function zipFilesAndDownload($file_names, $archive_file_name, $file_path) {
// Archive directory
$archiveDir = 'zips';
// Time-to-live
$archiveTTL = 86400; // 1 day
// Files to ignore
$ignoreFiles = array('.', '..');
// Loop the storage directory and delete old files
if ($dp = opendir($archiveDir)) {
while ($file = readdir($dp)) {
if (!in_array($file, $ignoreFiles) && filectime("$archiveDir/$file") < (time() - $archiveTTL)) {
unlink("$archiveDir/$file");
}
}
}
// Re-format the file name
$archive_file_name = "$archiveDir/".basename($archive_file_name);
// Create the object
$zip = new ZipArchive();
// Create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE) !== TRUE) {
exit("Cannot open '$archive_file_name'\n");
}
// Add each file of $file_name array to archive
foreach($file_names as $file) {
$zip->addFile($file_path.str_replace('./', '', $file), translit($files).".mp3");
}
$zip->close();
// Then send the headers to redirect to the ZIP file
header("HTTP/1.1 303 See Other"); // 303 is technically correct for this type of redirect
header("Location: http://{$_SERVER['HTTP_HOST']}/$archive_file_name");
exit;
}
Related
I have some encrypted responses that I convert to a Zip file in my Laravel application. The function below downloads the API response, saves it as a Zip file, and then extracts it while I read the folder's contents. In my local environment, it works well. However, the Zip file is not getting saved to the storage folder on the live server. No error is being shown, only an empty JSON response. Please, what could be the cause?
public function downloadZipAndExtract($publication_id, $client_id)
{
/* We need to make the API call first */
$url = $this->lp_store."clients/$client_id/publications/$publication_id/file";
$file = makeSecureAPICall($url, 'raw');
// Get file path. If file already exist, just return
$path = public_path('storage/'.$publication_id);
if (!File::isDirectory($path)) {
Storage::put($publication_id.'.zip', $file);
// Zip the content
$localArchivePath = storage_path('app/'.$publication_id.'.zip');
$zip = new ZipArchive();
if (!$zip->open($localArchivePath)) {
abort(500, 'Problems experienced while reading file.');
}
// make directory with the publication_id
// then extract everything to the directory
Storage::makeDirectory($publication_id);
$zip->extractTo(storage_path('app/public/'.$publication_id));
// Delete the zip file after extracting
Storage::delete($publication_id.'.zip');
}
return;
}
First thing I'd check is if the storage file is created and if it isn't created, create it. Then I'd look at your file permissions and make sure that the the groups and users permissions are correct and that you aren't persisting file permissions on creation. I've had many instances where the process that's creating files(or trying) is not in the proper group and there is a sticky permission on the file structure.
I tried to create a zip file using zip archive, it works fine although the zip file is downloaded twice in two different folders at same time, the htdocs folder where the source code is and the default download folder from browser setting. Is there any way that I could prevent this? I only want it to be downloaded once into download folder...
$file_names = explode(',', $_REQUEST['files']);
$dir = $_REQUEST['currentdir'];
//Archive name
$archive_file_name="Downloaded_".date("Y-m-d_G-i-s").".zip";
//Download Files path
$file_path=$dir;
//cal the function
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
$res = $zip->open($archive_file_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );
if ($res===TRUE) {
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$tt=$file_path."/".$files;
if (file_exists($tt)){
$zip->addFile($tt,$files);
}
else{
return false;
exit;
}
}
$zip->close();
//then send the headers to force download the zip file
header('Content-type: application/zip');
header("Content-Disposition: attachment; filename=\"".basename($archive_file_name)."\"");
header('Pragma: no-cache');
header('Expires: 0');
//header('Content-Length: ' . filesize($archive_file_name));
ob_end_clean();
//flush();
readfile($archive_file_name);
exit;
}
else{
return false;
exit;
}
}
Your PHP script creates the zip in the local directory, which is your htdocs directory.
You have a few options now:
Delete the zip archive after reading the archive with php (readfile)
Create the zip archive in a subdirectory, if you need to save it for later (archiving)
Create the zip in the temporary folder and delete the zip after use
I would go with option 3, as it will ensure that the zip gets ever deleted if the script dies before being able to delete it.
You can delete a file with the unlink() command. You just pass the filename or filepath to it and it does it (if the file exists). If you want to save it in a subdirectory, you just prepend the directory name with the directory separator to the filename. If you want to save it e.g. in the subdirectory 'downloads', you just add downloads/ before the filename. $archive_file_name="downloads/Downloaded_".date("Y-m-d_G-i-s").".zip";
The better option here is to create the zip in the server's temporary directory and manually delete it, so the zip gets cleaned up as soon as it's done. You will get the server's temporary directory with sys_get_temp_dir(), which you can prepend to the filename. After you're done with your business you can just delete the file with unlink().
$archive_file_name=sys_get_temp_dir()."Downloaded_".date("Y-m-d_G-i-s").".zip";
After you're done and want to delete the file you just do unlink($archive_file_name);.
Function reference:
http://php.net/unlink
http://php.net/sys_get_temp_dir
After download, you can delete the file on server:
(...)
ob_end_clean();
//flush();
readfile( $archive_file_name );
unlink( $archive_file_name );
(...)
I'm building an interface that allows collecting PDF-Files from a server-path to download them zipped at once which works fine most of the time. Now, some PDF-Files are not working, means if the zip contains one of the non-working files the whole download will not performed.
Now i tried many files with different sizes. I can tell most of the files larger than 10MB are not working, but one file (15mb) is working, so it can't be the file-size causing this error - right?
What happens is there's just no download at all. Additionally tried reading the results of "$zip->addFile" and "$zip->close" but it's all true, even if the download will not be performed. ANY ideas which may cause this bug or at least some hints on how to find this out?
The (x)debug inspection of the variables during process does not differ between the time it worked and the time its not working. Only differences are filesizes and -paths.
Here's the Code i'm using to build the ZIP:
public function downloadFiles() {
$myFiles = $this->Session->read('Files.MyFiles');
if(count($myFiles)) {
// build zip
$zipname = 'amaz_photos_' . time() . '.zip';
$zip = new ZipArchive();
$zip->open($zipname, ZipArchive::CREATE);
foreach($myFiles as $file) {
if($file['type'] == 'directory') {
$dirName = str_replace('/', '', substr($file['path'], strrpos($file['path'], '/'), (strlen($file['path']) - strrpos($file['path'], '/'))));
$zip->addEmptyDir($dirName);
$this->addFolderToZip($file['path'] . '/', $zip, $dirName . '/');
} else {
$zip->addFile($file['path'], str_replace('/', '', substr($file['path'], strrpos($file['path'], '/'), (strlen($file['path']) - strrpos($file['path'], '/')))));
}
}
$zip->close();
// get(send) zip
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
ignore_user_abort(true);
unlink($zipname);
}
// clear "myFiles"
//$this->Session->write('Files.MyFiles', array());
$this->redirect(array(
'controller' => 'pages',
'action' => 'display'
));
}
After all i tried opening the affected files with several PDF-Readers including Adobe's original to determine if its broken, but ̶i̶̶t̶̶s̶ it seems not.
This will occur in openable zip-files but the download does not start anyways. I found out that in that special cases where the download does not start, there is no response-header ("application/zip") being set on the network-frame compared to the times it worked.
What I found out else:
Lets say I have a folder with 20 files in it, that's not downloading. I tried selecting file 1-10: downloading, then I tried downloading file 11-20: downloading, then I tried to download file 1-20: not downloading, then I tried two times mixing the patterns, first the even files 2,4,6,8.. (1-20), after that the odd files 1,3,5,7,.. (1-20): each not downloading.
This states out the error does not belong to a specific file.
Any suggestions how to determine the trigger for this error?
Update:
I was able to download the generated zip-file doing these steps:
Trigger 'create & download zip-file'
It creates a zip-file but not download it
Type the generated zip-files filename in the code to download it on the next procedure (e. g. "$zipname = "generatedOnStep1.zip").
Trigger 'create & download zip-file' again
It does not download the (recently) generated file, but the file i generated in step 1. So it seems that: Generating Zip-Files is working AND downloading that same zip-File is working, but not at once and only in some unknown combinations.
...
Update 2:
Since I was not able to determine the issue nor fix it, I ended up redesigning the whole process, to redirect to the generated zip-file instead of "readfile" it. Some features had to be sourced out but anyways it works this way.
Try this... Remove readfile($zipname); and replace it with these:
/* make sure no other functions
holding this file */
$filehandle = fopen($zipname, "r");
while (!feof($filehandle)) {
/* lets read the file by pieces and
send it to user's browser */
echo fread($filehandle, 8192);
}
fclose($filehandle);
Hope this helps.
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
You need to be explicit with absolute paths when adding to the archive.
$zip->addFile($abs_path, $relative_path);
Solution: flush headers before calling readfile(). I learned that I do not need flush() but I left it in there anyways. 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.
$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.
Just to start, I'm a PHP noob.
I have an Apache server which hosts my files. I have a device which can only point to one PHP file. What I need it to do is have my PHP file read in the name of the file I want to download, and point it towards the directory it is stored. Currently, I have it pointing to one file, but I need it to be able to point to multiple. Is this possible in PHP?
Here's what I have so far:
<?php
$file_name = 'file.img';
$size = filesize($file_name);
$file_url = 'http://192.168.0.5/' . $file_name;
header("Content-length: $size");
header("Content-Type: text/plain");
header("Content-Transfer-Encoding: binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);?>
Edit:
The commands I want to input in order to download the file are close enough to as follows:
cmd=download+-a+$$.img+altimage
cmd=download+-a+$$.conf+altconfig
and the download directory is the .php file. I am open to other suggestions in how to do this.
Edit2:
Here's what an exact sample URL is:
myserver.com/cgi-bin/va/cmd?hdl+fullconfig.ini+altconfig
the hdl is a predefined function which points to the download directory, in order to download the file from the server, so the layout of what you mean isn't exactly the same.
I have trouble understanding what exactly you're trying to do, but I guess that you want a user to be able to download multiple files. If that is correct, here is one way to achieve this:
You can let PHP create a ZIP archive using the ZIP extension. For it to work, you have to load the extension php_zip.dll inside your php.ini.
$ZIP = new ZipArchive();
// Use the current time as the filename to prevent two users to use the same file
$ZIPName = microtime().".zip";
// Create a new ZIP file
$ZIP->open($ZIPName, ZIPARCHIVE::CREATE);
// Loop through all files - $Files needs to be an array with the names of the files you want to add, including the paths
// basename() will prevent the creation of folders inside the ZIP
foreach ($Files as $File) {
$ZIP->addFile($File, basename($File));
}
// Close the archive
$ZIP->close();
// Send the archive to the browser
readfile($ZIPName);
I hope this is what you were looking for.