我想按照 Laravel 專案中的視頻 ID 更改影像名稱。如何解決這個問題?
這是控制器的代碼
$old_video = Video::find($id);
//video thumbnail uploaded
if ($request->hasFile('image_path') != '') {
$video_themnull = $request->file('image_path');
$video_themnull_name = uniqid() . Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
$video_themnull_resize = Image::make($video_themnull->getRealPath());
$video_themnull_resize->resize(400, 200);
if ($video_themnull->isValid()) {
if (isset($old_video->image_path)) {
$files_old = $old_video->image_path;
if ( file_exists($files_old)) {
unlink($files_old);
}
}
$video_themnull_resize->save(public_path('video/themnull/' . $video_themnull_name));
$image_path = 'public/video/themnull/' . $video_themnull_name;
}
}
uj5u.com熱心網友回復:
在你的代碼中
$video_themnull_name = uniqid() . Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
您創建了隨機檔案名,如果您想用您的檔案名添加視頻 ID,那么您應該將 $old_video->id 添加到您的檔案名創建中。所以請重寫上面的代碼,如下所示
$video_themnull_name = $old_video->id. Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
并在 if ($video_themnull->isValid()) 條件中添加以下代碼
old_video->image_path = $image_path;
$old_video->save();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/364630.html
下一篇:按鍵拆分關聯陣列
