创建Zip和强制下载 - Laravel

Stu*_*ett 7 php zip laravel laravel-4

我正在使用Laravel 4.2,我正在设置一个区域,其中生成一个zip文件供用户下载.

我一直在收到错误

文件"myzip.zip不存在"

我目前的代码是:

// 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 );
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我,至于为什么我得到不存在的错误?

谢谢!

小智 4

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

创建文件下载响应

返回响应::下载($pathToFile);

返回响应::下载($pathToFile, $name, $headers);

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

$zip文件名
在你的最后一行代码中。

  • 在 5.2 中,这将是“return response()->download($pathToFile, $name, $headers);” (3认同)