Laravel not storing files from request

2019-07-29 07:25发布

问题:

I got form with file upload named "image". Form is working and i'm getting image but for some reason it's not stored. this is the code I use to store image:

$path = $request->image->store('storage/uploads');

before that I check

$request->hasFile('image')

and later I'm saving $path. Path is successfully saved and when i check it it's really storage/uploads/radnomid but there is no file

回答1:

Sorry for posting my own answer but problem was that by default laravel public disk does not upload to public directory. Settings needed to be changed like this: config/filesystems.php

  'public' => [
              'driver'     => 'local',
              'root'       => public_path(),
              'visibility' => 'public',
          ],

and then simply:

$path = $request->image->store('storage/uploads','public');


回答2:

Make sure that you are including enctype of multipart/form-data. You can either add the attribute directly to your form, or you can add a 'files' => true if you are using the FORM helper.

This is the conventional way to use the upload function that comes with Laravel 5.

public function processForm(Request $request) 
{
    if( $request->hasFile('image') ) {
        $file = $request->file('image');
        // Now you have your file in a variable that you can do things with
    }
}


回答3:

you can try with this code,

Image::make($request->file('image'))->save('upload_path/filename.jpg'));

here is complete doc of this Image package. http://image.intervention.io/



回答4:

Try doing like this

if ($request->hasFile('image')) {
   $image = $request->file('image');
   $filename = time() . '.' . $image->getClientOriginalExtension();
   $location = public_path('images/comment/' . $filename);
   Image::make($image)->resize(800, 600)->save($location);
}


回答5:

If your filesystems.php configs were default then

$path = $request->image->store('storage/uploads');

saved your files not in storage/uploads but in storage/app/storage/uploads.

And your current accepted answer is not really good because public_path() intended to be used for storing template files not user uploads. According to official docs uploads should be used in storage/ folder with a symbolic link from public/storage/ but not in public/.

Read more in https://laravel.com/docs/5.4/filesystem#the-public-disk



回答6:

I think you should try this:

Image::make($request->file('image'))->save(storage_path('uploads/image.jpg')); 

Hope this works for you!