Home > Enterprise >  Change image name and store image problem in Laravel project
Change image name and store image problem in Laravel project

Time:01-27

I want to change the image name follow video id in Laravel project. But when I upload and save the image, save at database name become /tmp/phpRXDHFh, I want the image name save at database as public/video/themnull/video-id.png. How to solve this? And also using below code, the image did not store at public/video/themnull folder. I do not know where got problem.

Here is code of controller

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;
    }

CodePudding user response:

use move function instead of storeAs

here is the documentation: https://laravel.com/docs/8.x/filesystem#copying-moving-files

something like below:

const PATH = 'your-address-in-public-folder';
$extention = $file->getClientOriginalExtension();
$filename = rand(111111, 999999) . "." . $image_extention;
$file->move($destination, $image_filename);

CodePudding user response:

I don't familiar with your method of saving the image. I will show you how I usually practice. Usually I will create new variable for the name, lets say like this:

$video_image_name = "public/video/themnull/video-id.png";

or if you want to add the id:

$video_image_name = "public/video/themnull/".$art_video->id.".png";

Then I will save it by calling the model. For example my model is MyImage that contain field name 'newName' and 'path'. This is how I save it:

MyImage::create([
     'newName'=> $video_image_name,
     'path'   => "public/video/themnull". $video_image_name,
]);
  •  Tags:  
  • Related