Creating Zip and Force Downloading

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

$zipFileName
in 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:

  • 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;
    
  • 链接地址: http://www.djcxy.com/p/85566.html

    上一篇: 在D3.js中使用自动文本大小进行可缩放圆形打包

    下一篇: 创建Zip和强制下载