Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Zip and Force Downloading - Laravel

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!

like image 769
StuBlackett Avatar asked Jun 03 '15 09:06

StuBlackett


2 Answers

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

$zipFileName
in your last line of code.
like image 167
Marco Vaccarini Avatar answered Sep 28 '22 19:09

Marco Vaccarini


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:

  • It will create a zip file by using ZipArchive Library.
  • Before creating a file it will check file exist or not.
  • If a file exists then it will remove the file.
  • If file not exists then it will create
  • a zip file with multiple passed for it.
  • 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;
    
like image 28
Bhaskar Bhatt Avatar answered Sep 28 '22 21:09

Bhaskar Bhatt