Creating Zip and Force Downloading - Laravel

18,738

Solution 1

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.

Solution 2

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;
    
Share:
18,738
StuBlackett
Author by

StuBlackett

Fullstack Developer. Work at Property Webmasters Use PHP Frameworks Laravel, Codeigniter and Zend to help create strong and reliable results for clients. Use LESS, SASS and Bootstrap for Frontend Work. Strong in Javascript & jQuery. Learning Vue.js as a side project

Updated on June 14, 2022

Comments

  • StuBlackett
    StuBlackett almost 2 years

    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!