![](/img/trans.png)
[英]Why am I getting error "Failure to create temporary file" when trying to close a zip file?
[英]I am trying to download multiple image as a zip file but getting error using laravel 7
我正在嘗試將多個圖像下載為 zip 文件,但出現錯誤foreach() 提供的參數無效,請幫助我如何解決該問題,謝謝。
檢查錯誤: https : //flareapp.io/share/47qG2A3m
public function dowloads($id)
{
$url = config('yourstitchart.file_url');
$zip = new ZipArchive;
$inboxFiles = Inbox::where('id', $id)->first()->file;
// $inboxFiles = "["phpCM0Yia.png","phptLC57a.png"]"
foreach ($inboxFiles as $file) {
$zip->add($url . $file); // update it by your path
}
$zip->close();
return response()
->download(
public_path('/temporary_files/' . "deals.zip"),
"deals.zip",
["Content-Type" => "application/zip"]
);
}
你正在返回一個字符串,你不能像數組一樣處理它。
它是 JSON,你可以使用:
$inboxFiles = json_decode(Inbox::where('id', $id)->first()->file);
(上面的代碼不是很健壯,但你有辦法)
我知道這已經回答了,但不要使用json_decode
在Laravel更多...
鑄造領域的file
作為JSON
/ array
,所以它會自動將一個數組,當你將它保存在數據庫中,它會轉化為JSON,當你想讀回,它會自動轉換為數組。 ..
為此,您必須編輯Inbox
模型並添加此屬性:
protected $casts = ['file' => 'array'];
就是這樣,然后您必須像使用該字段一樣使用該字段,就好像它已經是一個數組一樣,因此將您的代碼保留在您的問題中,無需任何編輯,它將立即起作用:
public function dowloads($id)
{
$url = config('yourstitchart.file_url');
$zip = new ZipArchive;
$inboxFiles = Inbox::where('id', $id)->first()->file;
// $inboxFiles = "["phpCM0Yia.png","phptLC57a.png"]"
foreach ($inboxFiles as $file) {
$zip->add($url . $file); // update it by your path
}
$zip->close();
return response()
->download(
public_path('/temporary_files/' . "deals.zip"),
"deals.zip",
["Content-Type" => "application/zip"]
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.