8

So I am trying to save uploaded image to laravel/public folder and return src so a path to where the file has been saved, however nothing happens after I choose an image and I get no errors, what might be wrong?

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


<form action="{{ action('BuilderController@testing') }}" enctype="multipart/form-data" role="form" method="POST">
    <input id="img" class="form-control filestyle margin images" data-input="false" type="file" data-buttonText="Upload Logo" data-size="sm" data-badge="false" onchange="uploadImage();" />
</form>
6
  • Please can you show the contents of uploadImage() also can show the the output for dd($request->allFiles())?] Commented Mar 6, 2017 at 11:16
  • nothing relevant there, I just tried to do it using different method function uploadImage() { var img = document.getElementById("img").src; alert(img); } and dd gives me nothing Commented Mar 6, 2017 at 11:19
  • You don't appear to be submitting the form?! Commented Mar 6, 2017 at 11:22
  • That is probobly the problem however I don't want to have a submit button, how can I do that using ajax? Commented Mar 6, 2017 at 11:23
  • dropzonejs.com Commented Mar 6, 2017 at 11:24

5 Answers 5

8

Try with move() method?

$filename = time().'.'.request()->img->getClientOriginalExtension();
request()->img->move(public_path('images'), $filename);

$user->image=$filename;
$user->save();
Sign up to request clarification or add additional context in comments.

Comments

1

you need to add name="img"on your form.

1 Comment

actually that was what he was missing
-1

You can easily upload image to public folder as following:

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

$input['imagename'] = time().'.'.$image->getClientOriginalExtension();

$destinationPath = public_path('/thumbnail');

$img = Image::make($image->getRealPath());

$img->resize(100, 100, function ($constraint) {

    $constraint->aspectRatio();

})->save($destinationPath.'/'.$input['imagename']);

/*After Resize Add this Code to Upload Image*/
$destinationPath = public_path('/');


$image->move($destinationPath, $input['imagename']);

And also you can check it here to resize and upload image:

http://itsolutionstuff.com/post/laravel-5-image-upload-and-resize-example-using-intervention-image-packageexample.html

Comments

-1

Make sure in the form you include enctype. Otherwise your file will not be submitted

<form action="#" method="POST" enctype="multipart/form-data">

</form>

Comments

-2

First, you have not added file input name

if(Input::file('inputname')){
//
}

Secondly,

For laravel-5.3 You can determine if a file is present on the request using the hasFile method

if ($request->hasFile('photo')) {
    //
}

And you can retrieve the uploaded file

$file = $request->file('photo');

You can find the complete documentation here

https://laravel.com/docs/5.3/requests#files

1 Comment

I get: Creating default object from empty value however image is being saved why?

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.