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

时间:2016-12-06 12:44:47

标签: laravel

我已使用此命令保存文件

$newFile = [
            'event_id' => $event->id,
            'path' => $storePath
           ];

EventFile::create($newFile);

我可以获得这样一个链接的文件路径:

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

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

5 个答案:

答案 0 :(得分:33)

Laravel 5

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

Laravel 4

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

答案 1 :(得分:5)

getClientSize() is deprecated starting version 4.1. Use getSize() instead.

https://github.com/symfony/symfony/blob/master/UPGRADE-4.1.md#httpfoundation

答案 2 :(得分:2)

laravel 8

  $imageSize = $request->file('file')->getSize();
$fil->size = number_format($imageSize / 1048576,2);
<块引用>

$file->size 我的表用你的数据库表改变它

答案 3 :(得分:1)

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

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

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

答案 4 :(得分:1)

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

use Illuminate\Support\Facades\Storage;

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

或者,如果您使用的是S3存储桶,则可以像下面这样修改上面的功能

use Illuminate\Support\Facades\Storage;

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

了解更多visit

相关问题