Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - get size from uploaded file

Tags:

laravel

I have saved a file with this command

$newFile = [             'event_id' => $event->id,             'path' => $storePath            ];  EventFile::create($newFile); 

I can get the path to the file for a link like this:

Storage::disk('public')->url($file->path); 

But there is no data about the file size. How can i get the file size in blade view???

like image 689
lewis4u Avatar asked Dec 06 '16 12:12

lewis4u


People also ask

How do I check the size of a file in Laravel 8?

Route::post('/files/add', 'FilesController@store')->name('files. store'); Then in your controller let's create a store method. Then that's it you will know if what is the byte size of your file and convert it to MB or KB.

How do I access storage files in Laravel?

Laravel's filesystem configuration file is located at config/filesystems.php . Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location.

How can I upload my file name in Laravel?

The hashName method is exactly what Laravel calls in the store method. $request->image->hashName(); You will get the same name that Laravel generates when it creates the file name during the store method. $path = $request->image->getClientOriginalName();


2 Answers

Laravel 5^

$request->file('file')->getSize(); 

Laravel 4

$request->file('file')->getClientSize(); // getClientSize() is deprecated in Laravel 5 
like image 125
Artur Subotkevič Avatar answered Sep 30 '22 10:09

Artur Subotkevič


The more Simpler way is to use Storage Facade if you have already stored / uploaded file

use Illuminate\Support\Facades\Storage;  public function get_size($file_path) {     return Storage::size($file_path); } 

Or if you are using S3 Bucket then you can modify the above function like below

use Illuminate\Support\Facades\Storage;  public function get_size($file_path) {     return Storage::disk('s3')->size($file_path); } 

Check Laravel File Storage

like image 37
Salman Zafar Avatar answered Sep 30 '22 09:09

Salman Zafar