laravel check file exists code example

Example 1: check if file exists laravel

$file = public_path('storage/images/' . $value)

if(! file_exists($file)){
  return asset('storage/images/default.png');
}

Example 2: laravel storage check file exists

Storage::disk('public')->exists($image)

Example 3: laravel create new file if not exists

if (!file_exists('somefile.txt')) {
    touch('somefile.txt', strtotime('-1 days'));
}

Example 4: assert file exists laravel

// From https://laravel-news.com/testing-file-uploads-with-laravel

Storage::disk('avatars')->assertExists('avatar.jpg');