Neeraj1005's avatar

Str::replace does not working while uploading the file

https://imgur.com/ePpl0i8 This is my file upload table where image stored as uploads/media\images.png. But I want to store like this uploads/media/images.png

if ($request->hasFile('image')) {
  if ($request->file('image')->isValid()) {
        $image = $request->file('image');
        $extension = $image->getClientOriginalExtension();//Getting extension
        $originalname = $image->getClientOriginalName();//Getting original name
       

        $path = $image->move('uploads/media/', Str::replaceLast("\", "/", $originalname));
       $imgsizes = $path->getSize();
        $size = getimagesize($path);
        $width = $size[0]; 
        $height = $size[1];
        $mimetype = $image->getClientMimeType();//Get MIME type
    }
}
//Start Store in Database
        $picture = new mediaLibrary();
        $picture->mime = $mimetype;
        $picture->imgsize = $imgsizes;
        $picture->original_filename = $originalname;
        $picture->extension = $extension;
        $picture->width = $width;
        $picture->height = $height;
        $picture->filename = $path;
        // $picture->filename = $image->getFilename().'.'.$extension;
        // $path = $image->storeAs('', $picture->original_filename);
        // $picture->filename = $filename;       
        $picture->save();
0 likes
5 replies
Tray2's avatar

Change this

Str::replaceLast("\", "/",

to this

Str::replaceLast("\\", "/",
Tray2's avatar

@snapey There are two \ in the code I wrote but for some reason only one is shown.

I guess that is some security thing. I've added another \ so it now displays two

Snapey's avatar

$original name should not contain any / or \ So I don't see the purpose of this code

Snapey's avatar

split this

$path = $image->move('uploads/media/', Str::replaceLast("\", "/", $originalname));

into

$image->move('uploads/media/', $originalname);
$path = 'uploads/media/' . $originalname;

Please or to participate in this conversation.