php - 创建 Zip 和强制下载 - Laravel

标签 php laravel laravel-4 zip

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

一直报错

The file "myzip.zip does not exist"

我当前的代码是:

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

谁能帮我,为什么我会收到不存在的错误?

谢谢!

最佳答案

来自 Laravel 文档:http://laravel.com/docs/4.2/responses#special-responses

创建文件下载响应

return Response::download($pathToFile);

return Response::download($pathToFile, $name, $headers);

也许你应该避免重复第二个

$zipFileName
在你的最后一行代码中。

关于php - 创建 Zip 和强制下载 - Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616066/

相关文章:

php - Laravel 的多个数据库配置文件

PHP - 使用新函数时如何避免旧服务器上的解析错误

Laravel:在 Controller 外部使用重定向?

laravel - 如何获取公共(public)目录?

php - Ubuntu 更改 sudo php 路径

laravel - 如何检查 Laravel 队列中是否有作业?

php - highchart 无法从 mysql 获取数据

php - PDO 类和 PDOStatement 类之间的关系

apache - 使用 Laravel 5.1 安装 phpMyAdmin

laravel - 如何安装 Laravel Vite?