Gujarat Santana
Gujarat Santana

Reputation: 10564

How to get the selected size of the file using faker in Laravel?

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?

Upvotes: 1

Views: 1558

Answers (3)

Gujarat Santana
Gujarat Santana

Reputation: 10564

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

Upvotes: 1

Bogdan Koliesnik
Bogdan Koliesnik

Reputation: 900

You can use php function getimagesize

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

Upvotes: 0

Skysplit
Skysplit

Reputation: 1943

You can use Illuminate\Http\UploadedFile which extends symfony's UploadedFile class

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

Upvotes: 1

Related Questions