Upload File/Image with class Storage Laravel 5.2
Solution 1
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()));
Solution 2
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);
}
Solution 3
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.
Comments
-
Darma Kurniawan Harefa almost 4 years
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 mypublic/img
directory. I useForm::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 -
Darma Kurniawan Harefa about 8 yearsThanks Matt It works, but I dont really undestand what happen on that put(). Can u explain to me? Thanks
-
Darma Kurniawan Harefa about 8 yearsThanks Juan but class File dosnt work for me. Need to define/use a File class?
-
Matt McDonald about 8 years@DarmaKurniawanHarefa The first argument passed to
put
is the full path for the file (as you've set a default folder in the config this can just be the name of the file if you don't want it in a subfolder). The second argument is the contents of the file. So using the phpfile_get_contents
function you're passing the file's contents to the Storageput
method. ThegetRealPath
method on your input file passes the real path of the temporary file that's been uploaded to your server. -
Juan Manuel Barea Martínez about 8 yearsIt wouldn't be necessary, but try to define in this way: ´use Illuminate/Contracts/Filesystem/Filesystem´ in your controller. Anyway, what error show you laravel?
-
Rkey over 7 yearsPerfect! Thanks :D
-
ssi-anik over 6 years\File::get($file); is that file or img?