1

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();
    }
}
2

3 Answers 3

2

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>
Sign up to request clarification or add additional context in comments.

4 Comments

still nothing happens
you must move file in server and then effort for Image::make() see this stackoverflow.com/questions/20805175/laravel-file-upload
I don't quite understand so my code is correct but I need to add code to move file? I thought $path will do that for me
Do not Debug mode ?
0

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>

2 Comments

Do you need a submit button? Is it possible to upload and display image upon selecting image in input?
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.