Laravel 5 Uploading and display image

2019-07-29 13:44发布

Basically I want to upload an image and display it to an user, at the moment when image is selected, nothing happens I get no error mistakes or anything else and I am wondering what is going on.

Here is my code:

<form action="{{ action('BuilderController@testing') }}" role="form" method="POST">
    <input class="form-control filestyle margin images" data-input="false" type="file" data-buttonText="Upload Logo" data-size="sm" data-badge="false" />
</form>

<div class="logo">
    <img class="images" id="image" src="#" alt="Your Logo"/>
</div>

Controller:

public function testing() {
    if(Input::file())
    {
        $image = Input::file('photo');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('images/' . $filename);
        Image::make($image->getRealPath())->resize(200, 200)->save($path);
        $user->image = $filename;
        $user->save();
    }
}

3条回答
Explosion°爆炸
2楼-- · 2019-07-29 14:41

you must move image to folder storage and insert name in table, try to change action to route url exemple action="{{ url('add/image')}}"

routes exemple

Route::post('add/image', 'your_controller_Image@testing');

controllers

public function testing(Request $request) {

  $insert_image = new App\NameTable();

  $file = $request->file('myfile_input');
  $path = public_path().'/images';
  $filename = time().'.'.$file->getClientOriginalExtension();

  if($file->move($path,$filename) )
    {
      $insert_image->column_name = $request->file($filename);
      $insert_image->save()
    }
}

form

<form enctype="multipart/form-data" method="post" action="{{ url('add/image')}}"> 
<input id="fileupload" name="myfile_input" type="file" /> 
<input type="submit" value="submit" id="submit" /> 
</form>
查看更多
\"骚年 ilove
3楼-- · 2019-07-29 14:42

you must using this code enctype="multipart/form-data" in html :

<form id="uploadbanner" enctype="multipart/form-data" method="post" action="#">
   <input id="fileupload" name="myfile" type="file" />
   <input type="submit" value="submit" id="submit" />
</form>
查看更多
你好瞎i
4楼-- · 2019-07-29 14:49

add image using ajax without submit form auto submit when select image

routes

Route::post('add/image',
[
  'as'=>'test.image.add',
  'uses'=>'Controller@testing' 
]);

Controllers

public function testing(Request $request)
{
  $insert_image = new App\NameTable();

  $file = $request->file('file_upload');
  $path = public_path().'/images';
  $filename = time().'.'.$file->getClientOriginalExtension();

  if($file->move($path,$filename) )
  {
    $insert_image->column_name = $request->file($filename);
    $insert_image->save();
    Session::flash('messageSuccess',"Success, add image successfully !!");
    return back();
  } 

}

View

<form  method="post" id="formimgInp" action="{{ route('test.image.add') }}" enctype="multipart/form-data">

   <input type="hidden" name="_token" value="{{ csrf_token() }}" />
   <input type="file" name="file_upload"  class="img_ava" id="imgInp"   />

</form>

<script>
document.getElementById("imgInp").onchange = function() {
    document.getElementById("formimgInp").submit();
}
</script>
查看更多
登录 后发表回答