davy_yg's avatar
Level 27

Upload Image

Hello,

I am trying to upload slide image and save it.

edit_home_slides.blade.php

<form method="post" action="{{ Url('/store_home_slides/'.'$list->id') }}" enctype="multipart/form-data">
    <input type="hidden" name="_token" value="{{ csrf_token() }}"/>
    <input type="hidden" name="id" value="{{ $list->id }}"/>
  <br>
  <table width="800">
      <tr>
          <td>Image </td>
          <td><img src="{{ $list->image }}" name="image"></td>              
      </tr>
      <tr>
          <td>Upload</td>
          <td>
              <input type="file" name="slides" id="slides">
          </td>
      </tr>
      <tr>
          <td>Title </td>
          <td><input required type="text" size="50" value="{{ $list->title }}" name="title"></td>
      </tr>          
      <tr>
          <td></td>
          <td><br><button type="submit">Submit</button></td>
      </tr>

  </table>  
</form>

PageController.php

public function store_home_slides(Request $request)
   {

    $id = $request->id;

    $this->validate($request, [
      'title' => 'required',
      'image' => 'required|image|mimes:jpg,jpeg,png|max:10000'
    ]);

    $slides = HomeSlidesModel::find($id);

    $slides->title = $request->title;
    $slides->image = $request->file('slides');

    if(Input::hasFile('slides'))
    {
        $file = $request->file('slides');
        //$file = $file->move(public_path().'/images/'.$file->getClientOriginalName());

        $path = $request->file('slides')->store('/images/'.$file->getClientOriginalName(), 'public');

        Session::flash('flash', 'Successfully save data');
    }

    $slides->update();

    $data = HomeSlidesModel::find($id);

    Session::flash('flash', 'Successfully save data');

    return view('admin.edit_home_slides')->with('list', $data);
}

web.php

Route::post('/store_home_slides/{id}', 'PageController@store_home_slides');

After clicking submit, I checked my database and frontview (no flash message) and there is no new image folder in my storage/app/public.

Anything wrong my codes?

ref: https://laravel.com/docs/6.x/filesystem#file-uploads

0 likes
7 replies
rameezisrar's avatar

@davy_yg make sure you have ran this artisan command at least once

php artisan storage:link

and then

if(Input::hasFile('slides'))
    {

 // make sure you have slides folder under/storage/app/public/
       $path = $request->file('slides')->store('slides');
    
\Log::info($path)
         Session::flash('flash', 'Successfully save data');
    }
davy_yg's avatar
Level 27

I did it and still does not works. I deleted today log file and try to upload the file again, there is no new log file appeared.

ftiersch's avatar
<form method="post" action="{{ Url('/store_home_slides/'.'$list->id') }}" 
  1. The method is called url() (lower case U)
  2. If you put $list->id in single quotes your URL will look like this: /store_home_slides/$list->id and that is probably not a valid URL in your project :)
  3. You call your input element "slides" but you validate for "image" which will be empty so you will get validation errors
1 like
davy_yg's avatar
Level 27

Okay, it's getting closer.

 if($slides)
    {
        $file = $request->file('slides');
      
        $path = storage_path($request->file('slides')->store('slides'));
        
    Log::info('path :'.$path);

        Session::flash('flash', 'Successfully save data');

    }

log file:

D:\xampp72\htdocs\aws_admin_test\storage\slides/OcSwmOdoRUDm4mt3FkjlhuWDsfMfMAESJiXAe1qw.jpeg

home.slides.blade.php

 @foreach($list as $item)  
          <tr>
            <td>{{ $item->id }}</td>
            <td><img src="{{ $item->image }}"></td>
            <td>{{ $item->title }}</td>
            
            <td align="center">
                <a href="{{ url('edit_home_slides').'/'.$item->id }}"><i class="fa fa-edit"></i> Edit</a><br> 
                <a href="{{ url('delete_job_detail').'/'.$item->id }}"><i class="fa fa-remove"></i> Delete</a> 
              </td>
          </tr>
          @endforeach

I did not see the image appears? seems like not found image. What should I do to make the path correct?

Virtualmix's avatar

You can use the storeAs() method if you prefer to specify a file name:

$path = $request->file('slides')->storeAs('/slides/', $file->getClientOriginalName(), 'public');

Edit: If you ran the command to create a symlink to your public directory, your file should be in app/storage/public/slides/example.jpg

davy_yg's avatar
Level 27

I let it using random file name so that it won't replace the file if other file has the same name. I just updated my post checked my post before this one.

@rameezisrar What is the point of running : php artisan storage:link ? I checked it is only to create storage folder in public directory just like a mirror to storage/app yet the file is not being copied to the public directory.

davy_yg's avatar
Level 27

I don' think that this is correct:

 $path = storage_path($request->file('slides')->store('slides'));

and this file from the $path cannot show the image:

D:\xampp72\htdocs\aws_admin_test\storage\slides/OcSwmOdoRUDm4mt3FkjlhuWDsfMfMAESJiXAe1qw.jpeg

After twiking the path, this is will works:

http:\localhost\aws_admin_test\storage\app\slides\OcSwmOdoRUDm4mt3FkjlhuWDsfMfMAESJiXAe1qw.jpeg

How can I get such a path?

Please or to participate in this conversation.