创建页面 - 更改图片名称问题 Laravel

Posted

技术标签:

【中文标题】创建页面 - 更改图片名称问题 Laravel【英文标题】:Create page - change image name problem Laravel 【发布时间】:2022-01-02 12:53:33 【问题描述】:

这是在创建页面下,想要在 Laravel 项目中根据视频 id 更改图像名称。但是当我在创建页面上传并保存图像时,保存在数据库名称变为/tmp/phpRXDHFh,我希望图像名称保存在数据库中为 public/video/themnull/video-id.png。如何解决?

这是控制器的代码

public function store(Request $request)


    $data = $request->validate([
        'video_path'   => 'nullable|mimes:mp4,avi,mpeg,ogx,oga,ogv,ogg,webm| max:25600',
        'user_id'      => 'required | integer',
        'category_id'  => 'required | integer',
        'video_section_id'  => 'required | string',
        'title'         => 'required | string',
        'post_date'    => 'nullable | date',
        'description'   => 'nullable | string',
        'youtube_link'  => 'nullable | string',
        'image_path'    => 'nullable | image |  mimes:jpeg,png,jpg,gif | max:2140',
    ]);


    if ($request->check_type == 1) 

        $data['type'] = 'link';
    

    $data['post_date'] = $request->post_date ?? date('Y-m-d');

    if ($request->hasFile('video_path') != '') 

        $art_video = $request->file('video_path');
        $ad_video_name = uniqid('video_') . Str::random('10') . '.' . $art_video->getClientOriginalExtension();
        $created_id = 0;

        $video_image_path = $request->file('image_path');
        $video_image_name =  $art_video->id. '.' . $video_image_path->getClientOriginalExtension();
        $video_image_path_resize = Image::make($video_image_path->getRealPath());
         $video_image_path_resize->resize(400, 200);
        if ($video_image_path->isValid()) 

            $video_image_path_resize->save(public_path('video/themnull/' . $video_image_name));
            $video_image_path = 'public/video/themnull/' . $video_image_name;
            $data['image_path'] = $video_image_path;
        

        if ($art_video->isValid()) 
            $fileName = $ad_video_name;
            $foldername = '/video';
            $name = $fileName;
            $video_path = $art_video->storeAs($foldername, $name, 'public');

            $data['youtube_link'] = null;
            $data['type'] = 'directly';
            $data['video_path'] = 'public/' . $video_path;
            $created_id = Video::create($data);
        
     else 
        $created_id = Video::create($data);
    
    if ($request->filled('tag_name')) 
        if ($created_id) 
            $last_inserted_id = $created_id->id;
            foreach ($request->tag_name as $tag_name) 
                $data2 = ([
                    'video_id' => $last_inserted_id,
                    'tag_name' => $tag_name
                ]);
                $created_id = VideoTag::create($data2);
            
        
    
    try 
        $this->successfullymessage('Video  Added successfully ');
        return redirect()->back();
     catch (\Exception $e) 
        $this->failmessage($e->getMessage());
        return redirect()->back();
    

【问题讨论】:

有人能解决这个问题吗? 【参考方案1】:
$extension = $request->file('image_path')->getClientOriginalName();
$fileName = time().'-'.$art_video->id..'.'.$extension; // you can use time with name if not, then use only $art_video->id..'.'.$extension;.
       
// $file->move('video/thumbnail',$fileName); to store in public folder

//  If you want to keep files in storage folder, you can use following : -

Storage::disk('public')->put('video/thumbnail/'.$fileName,File::get($request->file('image_path')));

// Dont't forget to run 'php artisan storage:link'
// It will store into your storage folder and you can access it by Storage::url('file_path)

【讨论】:

您好,我需要在哪里放置或更换? 从中修改图像名称和存储图像。 $video_image_path = $request->file('image_path'); $video_image_name = $art_video->id. '.' . $video_image_path->getClientOriginalExtension(); 把这个而不是 $video_image_path$video_image_name 放在顶部 $extension = $request->file('image_path')->getClientOriginalName(); $fileName = time().'-'.$art_video->id..'.'.$extension; 可能是您尝试将图像存储在这里 - $video_image_path_resize->save(public_path('video/themnull/' . $video_image_name)); 所以将以下内容替换到那里 - Storage::disk('public')->put('video/thumbnail/'.$fileName,File::get($request->file('image_path'))); 并检查您的存储文件。 嗨,仍然是数据库中存储的图像名称仍然 /tmp/phpPfJm8Q 我需要将图像名称存储在数据库 (image_path) 中,例如 public/video/themnull/video-id.png

以上是关于创建页面 - 更改图片名称问题 Laravel的主要内容,如果未能解决你的问题,请参考以下文章

控件实现点击预览图片更改页面背景图片的效果

WordPress:如何将类别页面标题更改为仅类别名称?

在上传图片 wordpress 上更改图片名称

需要在wordpress中更改页面标题

Wordpress:在管理员选项页面中上传图片

图像未使用 ID 名称保存