Php 如何在laravel 5.4中从数据库下载多个图像
在我的laravel 5.4应用程序中,我想从数据库下载所选产品的多个图像,该数据库正在Php 如何在laravel 5.4中从数据库下载多个图像,php,laravel,Php,Laravel,在我的laravel 5.4应用程序中,我想从数据库下载所选产品的多个图像,该数据库正在public_path中创建多个zip文件(“上传/下载图像”)但不下载文件如何下载此文件 下载此文件的最佳方式是什么 我做错了什么?有人能帮忙吗?先谢谢你 下面是我的代码 public function downloads_all(Request $request) { $public_dir = ''; $$zipFileName = ''; $zip = ''; $p
public_path中创建多个zip文件(“上传/下载图像”)代码>但不下载文件如何下载此文件
下载此文件的最佳方式是什么
我做错了什么?有人能帮忙吗?先谢谢你
下面是我的代码
public function downloads_all(Request $request)
{
$public_dir = '';
$$zipFileName = '';
$zip = '';
$product_images = DB::table('product_images')
->where('deleted_at', null)
->whereIn('product_id', explode(',', $request->ids))
->get();
if ($product_images)
{
foreach ($product_images as $value)
{
$zipFileName = 'product_' . $value->product_id . '_images.zip';
$zip = new ZipArchive;
$public_dir = public_path('uploads/download-images');
if (!is_dir($public_dir))
{
mkdir($public_dir, 0777, true);
}
if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE)
{
foreach ($product_images as $image)
{
$directory_path = base_path() . '/public/uploads/products/' . $image->product_id . '/images/';
$image_path = $image->image_path;
$file_name = basename($image_path);
$zip->addFile($directory_path . $file_name, $file_name);
}
$zip->close();
}
}
$files = File::files(public_path('uploads/download-images'));
$filecount = count($files);
$filetopath = array();
for ($i=0; $i < $filecount; $i++)
{
$filetopath = $public_dir . '/' . $zipFileName;
$headers = array(
'Content-Type' => 'application/octet-stream',
'Expires' => 0,
'Content-Transfer-Encoding' => 'binary',
'Content-Length' => filesize($filetopath),
'Cache-Control' => 'private, no-transform, no-store, must-revalidate',
'Content-Disposition' => 'attachment; filename="' . $zipFileName . '"',
);
if (file_exists($filetopath))
{
return response()->download($filetopath, $zipFileName, $headers)->deleteFileAfterSend(true);
}
else
{
return ['status'=>'zip file does not exist'];
}
}
}
else
{
return back();
}
}
public函数下载\u all(Request$Request)
{
$public_dir='';
$$zipFileName='';
$zip='';
$product\U images=DB::table('product\U images')
->其中('deleted_at',null)
->其中('product_id',explode(',',$request->id))
->get();
if($product\U图像)
{
foreach($product_图像为$value)
{
$zipFileName='product.'$value->product_id'.\u images.zip';
$zip=新的ZipArchive;
$public_dir=public_路径(“上传/下载图像”);
如果(!is_dir($public_dir))
{
mkdir($public_dir,0777,true);
}
如果($zip->open($public_dir.'/'.$zipFileName,ZipArchive::CREATE)==TRUE)
{
foreach($product_图像为$image)
{
$directory_path=base_path()。/public/uploads/products/。$image->product_id./images/;
$image\u path=$image->image\u path;
$file\u name=basename($image\u path);
$zip->addFile($directory\u path.$file\u name,$file\u name);
}
$zip->close();
}
}
$files=File::files(公共路径(“上传/下载图像”);
$filecount=count($files);
$filetopath=array();
对于($i=0;$i<$filecount;$i++)
{
$filetopath=$public_dir.'/'.$zipFileName;
$headers=数组(
“内容类型”=>“应用程序/八位字节流”,
“Expires”=>0,
'内容传输编码'=>'二进制',
“内容长度”=>filesize($filetopath),
“缓存控制”=>“专用,无转换,无存储,必须重新验证”,
“内容处置”=>“附件;文件名=“”。$zipFileName.”,
);
如果(文件_存在($filetopath))
{
return response()->download($filetopath,$zipFileName,$headers)->deleteFileAfterSend(true);
}
其他的
{
返回['status'=>'zip文件不存在'];
}
}
}
其他的
{
返回();
}
}
这是js
$(document).ready(function ()
{
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#download-master').on('click', function(e)
{
if($(this).is(':checked',true))
{
$(".multiple_download").prop('checked', true);
} else {
$(".multiple_download").prop('checked',false);
}
});
$('.download-all').on('click', function(e) {
var allVals = [];
$(".multiple_download:checked").each(function() {
allVals.push($(this).attr('data-id'));
});
if(allVals.length <=0)
{
alert("Please select row to download.");
}
else
{
var join_selected_values = allVals.join(",");
$.ajax({
url: $(this).data('url'),
type: 'POST',
data:'_token = <?php echo csrf_token() ?>',
data: 'ids='+join_selected_values,
success: function (data)
{
alert('Seccussfully Downloaded');
},
error: function (data) {
alert(data.responseText);
}
});
}
});
});
$(文档).ready(函数()
{
$.ajaxSetup({
标题:{
'X-CSRF-TOKEN':$('meta[name=“CSRF-TOKEN”]).attr('content'))
}
});
$(“#下载主机”)。在('click',函数(e)
{
如果($(this).is(':checked',true))
{
$(“.multiple_download”).prop('checked',true);
}否则{
$(“.multiple_download”).prop('checked',false);
}
});
$('.download all')。在('click',函数(e){
var-allVals=[];
$(“.multiple_download:checked”).each(function(){
allVals.push($(this.attr('data-id'));
});
如果(allVals.length$directory\u path
应该是绝对路径,例如,public\u path('/uploads/products/')
,或者$zip->addFile
无法读取文件的内容。试试这个
if (file_exists($filetopath))
{
return response()->download(storage_path($filetopath, $zipFileName, $headers));
}
$directory\u path适用于任何其他解决方案它可能适用于laravel模板刀片文件,因为web服务器将知道/
的确切含义,但在这种情况下/
不是web根目录。您可以检查$res=$zip->addFile($directory\u path.$file\u name,$file\u name)的返回值
,如果使用以/
开头的路径,则会得到false
,如果使用绝对路径,则效果良好。只需将$directory\u path='/public/uploads/products/';
替换为$directory\u path=public\u path('/uploads/products/')
可能是您需要检查文件是否确实存在,然后再将其添加到ZipArchive
,就像前面的答案一样。让我们来看看。@ShahidHussain您能解释一下为什么它不工作吗?它会产生什么样的错误?