我在创建和下载Zip文件时遇到了麻烦
我在这段代码上看到了 http://laravelcode.com/post/how-to-create-zip-file-in-laravel-using-ziparchive
这是我的控制器
public function download(){
$FOLDERID = $_GET['id'];
$FILEPATH = *My Database Query*
$PATHTOFILE = public_path().'/images/';
$ZIPNAME = 'ZIPFILENAME.zip';
$ZIP = new ZipArchive;
if ($ZIP->open($PATHTOFILE.'/'.$ZIPNAME, ZipArchive::CREATE) === TRUE) {
$FILES = *My Database Query to get file*
foreach ($FILES as $FILE) {
$ZIP->addFile($FILE->File_Path , $FILE->File_Name);
}
$ZIP->close();
}
else{
return "NO FILES CREATED";
}
我不知道这个人做了什么
$HEADERS = array(
'Content-Type' => 'application/octet-stream',
);
这个是下载zip
if(file_exists($PATHTOFILE)){
return Response()->download($PATHTOFILE, $ZIPNAME, $HEADERS);
}else{
return "asd";
}
仍然不知道#HEADERS做了什么
我在创建Zip文件时遇到问题,错误始终在$ ZIP-> close();
它说
ZipArchive :: close():读取错误:文件描述符错误
你能帮助我哪一个我做错了吗?提前谢谢!
答案 0 :(得分:0)
创建路线
Route::get('download-zip', 'ZipController@downloadZip');
步骤:2创建控制器 添加一种新的路由方法。 downloadZip()将生成新的zip文件并作为响应下载,因此让我们在下面添加:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
use ZipArchive;
class ZipController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function downloadZip()
{
$zip = new ZipArchive;
$fileName = 'myNewFile.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
$files = File::files(public_path('myFiles'));
foreach ($files as $key => $value) {
$relativeNameInZipFile = basename($value);
$zip->addFile($value, $relativeNameInZipFile);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
}