简体   繁体   中英

Upload File/Image with class Storage Laravel 5.2

First, I'm sorry for my bad English.

I want to upload a file/image from my driver to my project directory using class Storage . I want that every file/image will be uploaded/moved to my public/img directory. I use Form::file('img') on my views and on my post controller, I write this

 $img = Input::file('img');
    if ($img !== null) {
        $filename       = $img->getClientOriginalName();
        Storage::disk('uploads')->put('filename', $filename);

        $jenis->img     = $filename;  
    }

and on my config/filesystem I write this

'uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/img',
    ],

But, nothing happen on my public/img directory, no new file/image on there. Can u help me whats wrong with my code? and I hope u guys can help me with another good way on how to upload a file/image in laravel

Looks like your problem is you're not storing the file, you're referencing its name not its contents.

Try this:

Storage::disk('uploads') -> put($filename, file_get_contents($img -> getRealPath()));

In my filesystem file I configure my image directory in this way:

'uploads' => [
    'driver' => 'local',
    'root'   => public_path("/img"),
],

I think that you can use your way but is another point.

To get the file from your view you should use File::get Laravel function:

$filename = $img->getClientOriginalName();
Storage::disk('uploads')->put($filename, \File::get($file));

With this would be enough, you save the file with the name of file uploaded in directory specify in filesystem.

    if ($request->hasFile('original_pic')) {
            $original_pic = $request->file('original_pic');

            $file_extension=$original_pic>getClientOriginalExtension();
            $filename = time() . '.' . $file_extension;

            # upload original image
            Storage::put('ArticlesImages/' . $filename, (string) file_get_contents($original_pic), 'public');

            # croped image from request.
            $image_parts = explode(";base64,", $request->input('article_image'));
            $image_base64 = base64_decode($image_parts[1]);

            Storage::put('ArticlesImages/croped/' . $filename, (string) $image_base64, 'public');

            # get image from s3 or local storage.
            $image_get = Storage::get('ArticlesImages/croped/' . $filename);

            # resize 50 by 50 1x
            $image_50_50 = Image::make($image_get)
                    ->resize(340, 227)
                    ->encode($file_extension, 80);

            Storage::put('ArticlesImages/1x/' . $filename, (string) $image_50_50, 'public');

            $file_url = Storage::url('ArticlesImages/croped/' . $filename);

            return response()->json(['success' => true, 'filename' => $filename, 'file_url' => $file_url], 200);
        } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM