gpt4 book ai didi

php - 如何从 Laravel 5.4 中的数据库下载多个图像

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:40 26 4
gpt4 key购买 nike

在我的 laravel 5.4 应用程序中,我想从它在 public_path('uploads/download-images') 中创建多个 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();
}
}

这是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);
}
});
}
});
});

最佳答案

$directory_path 应该是绝对路径,例如public_path('/uploads/products/'),或者$zip->addFile 无法读取文件的内容。

关于php - 如何从 Laravel 5.4 中的数据库下载多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54108176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com