创建 Zip 和强制下载 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30616066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:42:24  来源:igfitidea点击:

Creating Zip and Force Downloading - Laravel

phplaravellaravel-4zip

提问by StuBlackett

I am using Laravel 4.2 and am looking to setup an area where a zip file is produced for the user to download.

我正在使用 Laravel 4.2 并希望设置一个区域,在该区域中生成一个 zip 文件供用户下载。

I keep getting the error

我不断收到错误

The file "myzip.zip does not exist"

文件“myzip.zip 不存在”

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!

谢谢你!

采纳答案by Marco Vaccarini

From Laravel documentation: http://laravel.com/docs/4.2/responses#special-responses

来自 Laravel 文档: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
在你的最后一行代码中。

回答by Bhaskar Bhatt

You can “create a zip of multiple files and download in PHP” below way.

您可以通过以下方式“创建多个文件的 zip 文件并在 PHP 中下载”。

To create a zip of multiple files and download with PHP it has some below features:

要创建多个文件的 zip 文件并使用 PHP 下载,它具有以下一些功能:

  • 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;
    
  • 它将使用 ZipArchive 库创建一个 zip 文件。
  • 在创建文件之前,它将检查文件是否存在。
  • 如果文件存在,那么它将删除该文件。
  • 如果文件不存在,那么它将创建
  • 一个带有多个传递的 zip 文件。
  • 当 zip 准备好后,它将自动下载。

    //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;