Laravel not storing files from request

2019-07-29 07:08发布

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

6条回答
看我几分像从前
2楼-- · 2019-07-29 07:46

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);
}
查看更多
走好不送
3楼-- · 2019-07-29 07:50

I think you should try this:

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

Hope this works for you!

查看更多
ゆ 、 Hurt°
4楼-- · 2019-07-29 07:57

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');
查看更多
做个烂人
5楼-- · 2019-07-29 08:01

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
    }
}
查看更多
爷的心禁止访问
6楼-- · 2019-07-29 08:10

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/

查看更多
▲ chillily
7楼-- · 2019-07-29 08:12

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

查看更多
登录 后发表回答