Php 如何将下载zip保存到laravel中的公用文件夹?
我想用下面的代码下载一个ZIP格式的文件夹 实际上,这只适用于下载文件夹到zip 但是我希望zip文件也保存到public/folder中的public文件夹。 我用拉威尔和齐帕奇 请帮帮我Php 如何将下载zip保存到laravel中的公用文件夹?,php,laravel,zip,add,ziparchive,Php,Laravel,Zip,Add,Ziparchive,我想用下面的代码下载一个ZIP格式的文件夹 实际上,这只适用于下载文件夹到zip 但是我希望zip文件也保存到public/folder中的public文件夹。 我用拉威尔和齐帕奇 请帮帮我 public function downloadzip($id_pra) { $dcmt = DB::table('document')->select(DB::raw(" max(id) as id"))->where('id_praapplication',$id_pra)-&g
public function downloadzip($id_pra) {
$dcmt = DB::table('document')->select(DB::raw(" max(id) as id"))->where('id_praapplication',$id_pra)->groupBy('type')->pluck('id');
$files = Document::whereIn('id', $dcmt)->get();
$url = url('')."/storage/uploads/file/".$id_pra."/";
# create new zip opbject
$zip = new \ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, \ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
$url2 = $url.$file->upload;
$url2 = str_replace(' ', '%20', $url2);
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$download_file = $output;
$type = substr($url2, -5, 5);
#add it to the zip
$zip->addFromString(basename($url.$file->upload.'.'.$type),$download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
ob_start();
$strFile = file_get_contents($tmp_file);
header('Content-disposition: attachment; filename=DOC-'.$id_pra.'-'.$url.'.zip');
header('Content-type: application/zip');
echo $tmp_file;
while (ob_get_level()) {
ob_end_clean();
}
readfile($tmp_file);
exit;
}
您可以直接创建ZIP文件,然后使用文件系统API下载,而不是创建临时文件
public function downloadzip($id_pra) {
$dcmt = DB::table('document')->select(DB::raw(" max(id) as id"))->where('id_praapplication',$id_pra)->groupBy('type')->pluck('id');
$files = Document::whereIn('id', $dcmt)->get();
$url = url('')."/storage/uploads/file/".$id_pra."/";
// create new zip object
$zip = new \ZipArchive();
// store the public path
$publicDir = public_path();
// Define the file name. Give it a unique name to avoid overriding.
$zipFileName = 'Documents.zip';
// Create the ZIP file directly inside the desired folder. No need for a temporary file.
if ($zip->open($publicDir . '/folder/' . $zipFileName, \ZipArchive::CREATE) === true) {
// Loop through each file
foreach($files as $file){
$url2 = $url.$file->upload;
$url2 = str_replace(' ', '%20', $url2);
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$download_file = $output;
$type = substr($url2, -5, 5);
#add it to the zip
$zip->addFromString(basename($url.$file->upload.'.'.$type),$download_file);
}
// close zip
$zip->close();
}
// Download the file using the Filesystem API
$filePath = $publicDir . '/folder/' . $zipFileName;
if (file_exists($filePath)) {
return Storage::download($filePath);
}
}
注意:
我会将CURL部分提取到一个方法
fetchFile($url)
返回下载的文件,但这超出了此问题的范围 您是否可以使用复制
,或文件内容
功能(或其他功能)将文件额外写入相关位置,然后发送文件?@常识,哦,对不起,我的代码中缺少了一些内容,已经为$dcmt更新了$file->upload
中存储的内容?