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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度