【问题标题】:Laravel - get size from uploaded fileLaravel - 从上传的文件中获取大小
【发布时间】:2017-04-21 02:43:56
【问题描述】:

我已经用这个命令保存了一个文件

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

EventFile::create($newFile);

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

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

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

【问题讨论】:

    标签: laravel


    【解决方案1】:

    根据Laravel Documentation 8.0 版:

    使用本地驱动时,所有应该可以公开访问的文件都应该放在 storage/app/public 目录下。

    所以这里本地存储的rootstorage/app/中的public/目录

    您可以像这样在文件模型中指定获取文件大小的方法

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Support\Facades\Storage;
    
    
    class File extends Model
    {
        use HasFactory;
        protected $fillable = [
            'name',
            'file_path'
        ];
    
        public function getFileSize() 
        {
            // Ensure $this->file_path begins with '/public/';
            return Storage::size($this->file_path);
        }
    }
    

    在你的刀片文件中像这样使用它

    <div>{{ $file->getFileSize() }}</div>
    

    如果你这样使用,$this-&gt;file_path必须是格式:/public/optional_subdirectory/file_name

    【讨论】:

      【解决方案2】:

      laravel 8

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

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

      【讨论】:

        【解决方案3】:

        Laravel 5^

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

        Laravel 4

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

        【讨论】:

        • 在刀片视图中没有 $request 变量
        • 嗯,你需要在你的控制器中调用它并将它传递给数据库或刀片视图......
        • 是的,我想通了。我为文件大小创建了一个新表列,创建文件时我也保存文件大小......所以我可以调用 $file->size
        • 我想知道返回的整数是用什么单位表示的,我的意思是kb,mb,bits,bytes?
        • getClientSize() 返回文件大小(以字节为单位)
        【解决方案4】:

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

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

        或者如果你使用S3 Bucket,那么你可以像下面这样修改上面的函数

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

        查看Laravel File Storage

        【讨论】:

        • 但是如果文件还没有上传并且在服务器上,你不能这样做......我指的是实际请求中的文件......它将被上传。
        【解决方案5】:
        getClientSize() is deprecated starting version 4.1. Use getSize() instead.
        

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

        【讨论】:

        • 我不明白你的意思。您能否按照a good answer 的发布指南进行操作
        • 好吧,我相信从 Laravel 5.7 开始你应该改用 getSize() 方法。
        【解决方案6】:

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

        //add this at the top of your controller
        use File;
        
        // add this with in your function
        $size = File::size($PATH_OF_FILE);
        

        【讨论】:

        • 但是如果文件还没有上传并且在服务器上,你不能这样做......我指的是实际请求中的文件......它将被上传。
        猜你喜欢
        • 2011-05-10
        • 2011-11-21
        • 2019-11-04
        • 2020-10-17
        • 2014-01-06
        • 1970-01-01
        相关资源
        最近更新 更多