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

时间:2016-08-23 10:21:25

标签: php laravel laravel-5.2

我使用fzaninotto/faker在我的项目文件中创建了一个工厂,如下所示:

      '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,

我需要从$ faker-&gt; file()中获取所选文件的大小,就像我在{MBA>字段download_url中看到的那样,文件扩展名为 .mp4 < / strong>可以这样做吗?

3 个答案:

答案 0 :(得分:1)

您可以使用Illuminate\Http\UploadedFile扩展symfony的UploadedFile

$path = $faker->file($sourceDir='/home/gujarat/fakerFile/video', $targetDir='./public/videoLibrary', false);
$uploadedFile = new Illuminate\Http\UploadedFile($path, 'randomName');
$uploadedFile->getClientSize();

答案 1 :(得分:0)

您可以使用php函数getimagesize

list($width, $height) = getimagesize("dir/image.jpg");

答案 2 :(得分:0)

首先找到文件的路径,然后使用here中的filesize()来确定文件的大小。 我设法在我的工厂类中这样做:

$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,
  ];
});