Laravel - 从上传的文件中获取大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40995731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:35:04  来源:igfitidea点击:

Laravel - get size from uploaded file

laravel

提问by lewis4u

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???

但是没有关于文件大小的数据。如何在刀片视图中获取文件大小???

回答by Artur Subotkevi?

Laravel 5

Laravel 5

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

Laravel 4

Laravel 4

$request->file('file')->getClientSize(); // getClientSize() is deprecated in Laravel 5

回答by MD. Shafayatul Haque

Very simple(Proper Laravel Way):

非常简单(正确的 Laravel 方式):

//add this at the top of your controller
use File;

// add this with in your function
$size = File::size($PATH_OF_FILE);

回答by Salman Zafar

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

Storage Facade如果您已经存储/上传文件,则更简单的方法是使用

use Illuminate\Support\Facades\Storage;

public function get_size($file_path)
{
    return Storage::size($file_path);
}

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

或者,如果您使用的是S3Bucket,那么您可以像下面一样修改上述功能

use Illuminate\Support\Facades\Storage;

public function get_size($file_path)
{
    return Storage::disk('s3')->size($file_path);
}

to know more visit

了解更多访问