I am using Laravel 4.2 and am looking to setup an area where a zip file is produced for the user to download.
I keep getting the error
The file "myzip.zip does not exist"
My current code is :
// Here we choose the folder which will be used.
$dirName = public_path() . '/uploads/packs/'.$pack_id;
// Choose a name for the archive.
$zipFileName = 'myzip.zip';
// Create "MyCoolName.zip" file in public directory of project.
$zip = new ZipArchive;
if ( $zip->open( public_path() . '/' . $zipFileName, ZipArchive::CREATE ) === true )
{
// Copy all the files from the folder and place them in the archive.
foreach ( glob( $dirName . '/*' ) as $fileName )
{
$file = basename( $fileName );
$zip->addFile( $fileName, $file );
}
$zip->close();
$headers = array(
'Content-Type' => 'application/octet-stream',
);
// Download .zip file.
return Response::download( public_path() . '/' . $zipFileName, $zipFileName, $headers );
Can anyone help me, As to why I am getting the does not exist error?
Thank You!
From Laravel documentation: http://laravel.com/docs/4.2/responses#special-responses
Creating A File Download Response
return Response::download($pathToFile); return Response::download($pathToFile, $name, $headers);
Maybe you should avoid to repeat the second
$zipFileNamein your last line of code.
You can “create a zip of multiple files and download in PHP” below way.
To create a zip of multiple files and download with PHP it has some below features:
When the zip is ready it will be auto download.
//get path of files
$path = base_path();
$file_path = $path."/../uploads/advatise/";
//getting data from database
$row = $this->getRow($ids);
//if data exist
if($row) {
//multiple images in single attrubute
$images=$row->advertise_image;
$images=explode(",",$images);
//creating zip object
$zip = new ZipArchive();
//creating file name
$DelFilePath="images".$row->id.".zip";
//if file exists then to delete it
if(file_exists($file_path.$DelFilePath)) {
unlink ($file_path.$DelFilePath);
}
//if not exist then to create zip file
if ($zip->open($file_path.$DelFilePath, ZIPARCHIVE::CREATE)
!= TRUE) {
die ("Could not open archive");
}
//loop on the images/file to add in zip
foreach ($images as $key => $image) {
$zip->addFile($file_path.$image,$image);
}
// close and save archive
$zip->close();
//opening zip and saving directly on client machine
header("Location:".Request::root()."/uploads/advatise/".$DelFilePath);
}
exit;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With