我正在使用 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/