skip to Main Content

I’m learning and tried to follow a bunch of guides but I just can’t seem to store it in the right directory if at all.

So my storage structure is like this:
storage -> app-> public -> books

Please tell me how I can store an image named $isbn in the ‘books’ folder like storage -> app-> public -> books -> $isbn.jpg

Thank you.

I tried:

$image = $request->file('imgInp');
Storage::disk('local')->put('books/'.$isbn, $image, 'public');

All this did was store the image (that is not named $isbn) like storage -> app-> books -> $isbn -> image_with_weird_name.jpg.

3

Answers


  1. Try this:

    $request->file('imgInp')->storeAs(
        'books', $isbn, ['disk' => 'public']
    );
    

    Or:

    Storage::disk('public')->putFileAs(
        'books', $request->file('imgInp'), $isbn
     );
    
    Login or Signup to reply.
  2. try this

    $image = $request->file('imgInp')
    Storage::disk('public')->putFileAs('books', $image, $isbn);
    
    Login or Signup to reply.
  3. You can use getClientOriginalExtension() method to get the extension of the original file and concatenating it with the $isbn to form the filename.

    $image = $request->file('imgInp');
    $filename = $isbn . '.' . $image->getClientOriginalExtension(); // set filename to $isbn.jpg
    Storage::disk('public')->putFileAs('books', $image, $filename);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search