Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在没有临时文件的情况下强制zip加载?_Php_Laravel_Public_Ziparchive_Tmp - Fatal编程技术网

Php 如何在没有临时文件的情况下强制zip加载?

Php 如何在没有临时文件的情况下强制zip加载?,php,laravel,public,ziparchive,tmp,Php,Laravel,Public,Ziparchive,Tmp,我想做动态压缩,所以当管理员点击.zip时,它会根据用户ud下载并压缩文件夹中的所有文件 用户将在其id为的文件夹中拥有文档和存储 i、 e/public/stroage/file/ID001,因此当用户单击“下载”按钮时,它将强制下载ID0001.zip 这是我的密码 $zip=new\ZipArchive(); $uploaddir=public_path()。/temp'; $tmp_file=tempnam($uploaddir,”); $zip->open($tmp\u文件,\Zip

我想做动态压缩,所以当管理员点击.zip时,它会根据用户ud下载并压缩文件夹中的所有文件 用户将在其id为的文件夹中拥有文档和存储

i、 e/public/stroage/file/ID001,因此当用户单击“下载”按钮时,它将强制下载ID0001.zip

这是我的密码

$zip=new\ZipArchive();
$uploaddir=public_path()。/temp';
$tmp_file=tempnam($uploaddir,”);
$zip->open($tmp\u文件,\ZipArchive::CREATE);
//循环浏览每个文件
foreach($files作为$file){
$url2=$url.$file->upload;
$url2=str_replace('',%20',$url2);
如果(!function_存在('curl_init')){
模具('未安装卷曲!');
}
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$output=curl\u exec($ch);
卷曲关闭($ch);
$download_file=$output;
$type=substr($url2,-5,5);
#把它加到拉链上
$zip->addFromString(basename($url.$file->upload.'.$type),$download\u file);
}
//拉链
$zip->close();
//定时下载
日期默认时区设置(“亚洲/吉隆坡”);
$time_name=日期('Ymd');
$time_download=日期('Y-m-d H:i:s');
$log\u download=新日志下载;
$user=Auth::user();
$id\u user=$user->id;
$log\u download->id\u pra应用程序=$id\u pra;
$log\u download->id\u user=$id\u user;
$log_download->Activity='download ZIP File';
$log_download->type='Zip存档';
$log\u download->download\u at=$time\u download;
$log_下载->保存();
#将文件作为下载发送到浏览器
ob_start();
$strFile=文件获取内容($tmp\u文件);
标题('Content-disposition:attachment;filename=DOC-'.$id_pra.-'..zip');
标题(“内容类型:应用程序/zip”);
echo$tmp_文件;
while(ob_get_level()){
ob_end_clean();
}
readfile($tmp_文件);
//$filetopath=$public_dir.'/'.$zipFileName;
//$filetopath=public_path()。/storage/uploads/file/$id_pra.'/.$zipFileName;
//创建下载响应
//如果(文件_存在($filetopath)){
//return response()->download($filetopath、$zipFileName、$headers);
//} 

退出您应该能够执行
$zip->open命令php://output“,\ZipArchive::CREATE)。完整代码:

   header('Content-disposition: attachment; filename=DOC-'.$id_pra.'-'.'.zip');
   header('Content-type: application/zip');
   $zip = new \ZipArchive();
   $zip->open('php://output', \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();
            

        
// Set Time Download
date_default_timezone_set("Asia/Kuala_Lumpur");
$time_name = date('Ymd');
$time_download = date('Y-m-d H:i:s');
$log_download = new Log_download;
                
$user = Auth::user();
$id_user =  $user->id;
$log_download->id_praapplication   =  $id_pra;
$log_download->id_user   =  $id_user;
$log_download->Activity   =  'Download ZIP File';
$log_download->type   =  'Zip Archive';
$log_download->downloaded_at   =  $time_download;
$log_download->save();

exit;

但是,zip文件大小不应超过PHP最大内存限制。如果是这样的话,你将得到损坏的zip文件。

你应该能够执行
$zip->open>操作php://output“,\ZipArchive::CREATE)。完整代码:

   header('Content-disposition: attachment; filename=DOC-'.$id_pra.'-'.'.zip');
   header('Content-type: application/zip');
   $zip = new \ZipArchive();
   $zip->open('php://output', \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();
            

        
// Set Time Download
date_default_timezone_set("Asia/Kuala_Lumpur");
$time_name = date('Ymd');
$time_download = date('Y-m-d H:i:s');
$log_download = new Log_download;
                
$user = Auth::user();
$id_user =  $user->id;
$log_download->id_praapplication   =  $id_pra;
$log_download->id_user   =  $id_user;
$log_download->Activity   =  'Download ZIP File';
$log_download->type   =  'Zip Archive';
$log_download->downloaded_at   =  $time_download;
$log_download->save();

exit;
但是,zip文件大小不应超过PHP最大内存限制。如果是这样的话,你最终会得到损坏的zip文件