douyi6922 2016-08-23 10:21
浏览 163
已采纳

如何在Laravel中使用faker获取文件的选定大小?

I have created a factory in my project file using fzaninotto/faker like this :

      'icon' => $faker->file($sourceDir='/home/gujarat/fakerFile/images', $targetDir='./public/iconLibrary', false),
      'title' => $faker->name,
      'date' => $faker->numberBetween($min = 1900, $max = 2016),
      'size' => $faker->randomFloat($nbMaxDecimals = 10.0, $min = 2.0, $max = 10.0),
      'file_type' => 'video/mp4',
      'download_url' => $faker->file($sourceDir='/home/gujarat/fakerFile/video', $targetDir='./public/videoLibrary', false),
      'category_id' => $randomCategory->id,

I need to get the size of the selected file from $faker->file() as you can see in my field download_url in MB or KB, and the file extension is .mp4 is it possible to do this?

  • 写回答

3条回答 默认 最新

  • dongtaigan1594 2016-08-24 03:33
    关注

    First find the path of the file, and use filesize() from here to determine the size of the file. I manage to do this in my factory class like this :

    $factory->defineAs(App\Library::class, 'library_video', function (Faker\Generator $faker) {
      $categories = Category::all();
      $randomCategory = $categories->random();
    
      $randomFile = $faker->file($sourceDir='/home/gujarat/fakerFile/video', $targetDir='./public/videoLibrary', false);
      $size = filesize("./public/videoLibrary/".$randomFile);
    
    
      return [
          'icon' => $faker->file($sourceDir='/home/gujarat/fakerFile/images', $targetDir='./public/iconLibrary', false),
          'title' => $faker->name,
          'date' => $faker->numberBetween($min = 1900, $max = 2016),
          'download_url' => $randomFile,
          'size' => $size,
          'file_type' => 'video/mp4',
          'category_id' => $randomCategory->id,
      ];
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题