【问题标题】:Update page - Change image name follow video id Laravel更新页面 - 更改图像名称跟随视频 ID Laravel
【发布时间】: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


【解决方案1】:

在您的代码中

$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(); 

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 2020-03-20
    • 2018-09-30
    • 2021-02-21
    • 2013-03-16
    相关资源
    最近更新 更多