[英]How to save images with different names?
我正在處理有關多張圖片上傳的一些問題。 我的項目處理用戶輸入多個圖像並保存到數據庫的表單,但首先將圖像臨時存儲在目錄中以顯示各種預覽。 在我的 class 中,它處理圖像的上傳,我試圖重命名圖像,如下所示。
我在下拉列表中上傳了七張圖片。
我看到它的所有圖像都被一個存儲在數據庫中。
雖然它必須以不同的名稱存儲。
Controller
public function store(Product $product, Request $request)
{
$path = null;
if ($request->hasFile('file'))
{
$file = $request->file('file');
$name = time();
$extension = $file->getClientOriginalExtension();
$fileName = $name . '.' . $extension;
$path = $file->storeAs($this->path, $fileName, 'public');
}
$gallery = Gallery::create([
'product_id' => $product->id,
'image' => $path,
]);
return response()->json($gallery);
}
$file = $request->file('file');
$name = date('YmdHi');
$extension = $name.$file->getClientOriginalExtension();
controller中,替換即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.