How to exclude file path from downloaded file name using PHP ZipArchive - php

I'm trying to create a zip archive from some files using PHP.
$zipname = 'path/to/my/dir/' . uniqid('MyZip', true) . '.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$dl_file = file_get_contents($file);
$zip->addFromString(basename($file),$dl_file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
unlink($zipname);
It creates an archive just fine in the correct location named something to the extent of: MyZip124153.zip
However, the file my browser downloads is named path-to-my-dirMyZip124153.zip.
I'm not sure how I can prevent it from doing this. I'd prefer to not have this filesystem info embedded in these filenames.
Thank you!

Obviously it is going to put the path inside a file name , as you are using the path string as filename inside the header.
Change your :
header('Content-disposition: attachment; filename=' . $zipname);
to something like :
$new_file_name = 'myzipfile.zip';
header('Content-disposition: attachment; filename=' . $new_file_name);

Related

download multiple files as zip in php without save on host

I use this way for download multiple files as zip :
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
But the file file.zip be save on my host . I don't want to save on my host i just wanna download it withou save .
Thank

Zip google cloude storage file damage

I use the following code to zip the google file, and the zip file is created successful, but after double clicked show error message as screenshot. How to I solve it?
$zip = new ZipArchive();
$zipname = time() . ".zip"; // Zip name
$zip->open($zipname, ZipArchive::CREATE);
$files = array('http://lh3.googleusercontent.com/k8tmo5m0KZLJbZjLiSMHjefsjJXfQSS_r6julCj8LCW-OsOFf715YOlWvHE9Xze4Rm2gxsWhl9OCLLHsXyluyDFz4n1xRQHOijS3iOs',
'http://lh3.googleusercontent.com/hk_y_5BKSCJpNOqpCkq7mWTPzljo8kXmLf2qQR16K6HASfEeDu0yxNBcheFyjnFUCMOl6-fZiJlp4n4Kb_fhihUGyn58TUDRD1iECrur');
foreach ($files as $file) {
$zip->addFromString(basename($file), file_get_contents($file));
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

Download mp3s from url array to zip file

I have an array of links to mp3 files. I need to have all of the mp3s downloaded from a server and then zipped.
This code below is not working:
Any suggestions?
$audio_flies = array('http://example.com/1.mp3','http://example.com/2.mp3');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($audio_files as $file) {
echo "$file";
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The archive is either in unknown format or damaged
You first need to download those files and store them locally on your server. Once that is done you will be able to zip them and make the zip file available for download.
The most basic way of achieving this is by using PHP's file_get_contents(), file_put_contents() and parse_url().
This code has not been thoroughly tested.
// Fixed the name of the array
$audio_files = array('http://example.com/1.mp3', 'http://example.com/2.mp3');
// You need to change this to
// a path where you want to store your
// audio files (Absolute path is best)
$local_path = '/local/path/here/';
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
// Download the files
// Not an elegant way to
// download files
foreach ($audio_files as $file) {
// First let's get the file name
// by parsing the URL
// and trimming the '/'
$file_name = ltrim(parse_url($file, PHP_URL_PATH), '\/');
// Download the file
$a_contents = file_get_contents($file);
// Place the downloaded content
// in the defined local path
file_put_contents($local_path . $file_name, $a_contents);
// Add the file to archive
$zip->addFile($local_path . $file_name);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
This is a very bad way to achieve your task for the following reasons (among many others):
There is no timeout for the download process
If a file fails to download it will halt the execution
It may take a very long time (depending on the size of the audio files) to serve the zip file
There is no proper error / exception handler
Many other reasons regarding structure that I'm not gonna go through here.
try the following: use parse_url to get the actual filename, use file_get_contents and file_put_contents to download and save the data locally...
$audio_flies = array('http://example.com/1.mp3','http://example.com/2.mp3');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($audio_files as $file) {
echo "$file";
$parsed = parse_url($file);
$localfile = "." . $parsed["path"];
$mp3 = get_file_contents($file);
file_file_contents($localfile, $mp3);
$zip->addFile($localfile);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

PHP Create ZIP file in background

I am using the following script to create zip files: http://davidwalsh.name/create-zip-php
It's working just fine and does exactly what I need. Sometimes though I have large files and zipping those takes up to 30 seconds.
I want to let the user know that their download is being packed and tell them whenever the download is ready.
How would I do that? I have a script which is called download.php which basically just calls this create_zip method from above and then forces a download like this:
$files = explode(":", $file_decrypted);
array_pop($files);
$zipname = 'uploads/download_' . $username . '.zip';
foreach ($files as &$value) {
$value = "uploads/" . $username_d . "/" . $value;
}
$resultat = create_zip($files, $zipname, 'uploads/' . $username . '/');
ob_clean();
ob_end_flush();
// Start the download for the user
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
exit();
So basically I want to show a text to the user (For example: Preparing download...) and at the same time call all the above php stuff in the background somehow.

Change storage folder when create zip

I use this code to create ZIP file from PHP
ZIP is correctly created, but I have a little trouble when download zip.
On browser, ZIP name appear like this
.._.._storage_temp_2013-09-03-1378245354.zip
I'm like to mantain only
2013-09-03-1378245354.zip
Here code I use:
$files = array($frente, $verso);
//$zipname = '../../storage/file.zip';
$zipname = "../../storage/temp/".date('Y-m-d')."-".time().".zip"; // Zip name
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile($file,$new_filename);
//$zip->addFromString(basename($file), file_get_contents($file));
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
ob_clean();
flush();
readfile($zipname)
I have solved in this way:
$new_zipname = substr($zipname,strrpos($zipname,'/') + 1);
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$new_zipname);

Categories