【发布时间】:2022-01-02 12:04:41
【问题描述】:
我想根据 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;
}
}
【问题讨论】:
-
在 if ($video_themnull->isValid()) 条件中添加以下代码 $old_video->image_path = $image_path; $old_video->save();
-
嗨@Mahesh,这可以根据视频ID更改图像名称吗?这段代码需要在哪里替换或添加?
-
在 $image_path = 'public/video/themnull/' 之后。 $video_themnull_name;
-
嗨@Mahesh,谢谢,但图像名称仍然是随机创建的,我希望图像名称更改跟随视频ID。
-
在您的代码 $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();
标签: laravel file-upload