Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PetroGromovo's avatar

Why spatie/laravel-medialibrary raise error on uploding image uploaded with faker-images?

I added https://github.com/morawskim/faker-images to laravel 9 site and when I try get image from this lib and to upload it with Medialibrary

$faker = \Faker\Factory::create();

$faker->addProvider(new \Mmo\Faker\PicsumProvider($faker));
$faker->addProvider(new \Mmo\Faker\LoremSpaceProvider($faker));

 $pageUploadedImageFile= $faker->picsumUrl(400, 400);

$imageMedia = $this->modelItem
     ->addMediaFromUrl($pageUploadedImageFile)
     ->usingFileName($imageFilename)
     ->toMediaCollection('media');

where $pageUploadedImageFile has value like :

https://picsum.photos/400/400?random=83611  

which is readable in my browser

But I got error :

  Spatie\MediaLibrary\MediaCollections\Exceptions\UnreachableUrl 

  Url `https://picsum.photos/400/400?random=83611` cannot be reached

  at vendor/spatie/laravel-medialibrary/src/MediaCollections/Exceptions/UnreachableUrl.php:9
      5▕ class UnreachableUrl extends FileCannotBeAdded
      6▕ {
      7▕     public static function create(string $url): self
      8▕     {
  ➜   9▕         return new static("Url `{$url}` cannot be reached");
     10▕     }
     11▕ }
     12▕ 

What is wrong and how that can be fixed ?

"laravel/framework": "^9.26.1",
"laravel/tinker": "^2.7.2",
"spatie/laravel-medialibrary": "^10.4.4",
"spatie/laravel-permission": "^5.5.5",
"squizlabs/php_codesniffer": "*"

"barryvdh/laravel-ide-helper": "^2.12",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mmo/faker-images": "^0.6.0",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^6.1",

I tried to debug source of the package and I found that error israised in file src/Downloaders/DefaultDownloader.php:

<?php

namespace Spatie\MediaLibrary\Downloaders;

use Spatie\MediaLibrary\MediaCollections\Exceptions\UnreachableUrl;

class DefaultDownloader implements Downloader
{
    public function getTempFile(string $url): string
    {
        \Log::info( ' getTempFile $url::');
        \Log::info( $url);
        $stream = fopen($url, 'r');
        \Log::info( ' getTempFile $stream::');
        \Log::info( $stream);
        if (! $stream ) {
            throw UnreachableUrl::create($url);
        }

        $temporaryFile = tempnam(sys_get_temp_dir(), 'media-library');

        file_put_contents($temporaryFile, $stream);

        return $temporaryFile;
    }
}

and I see in log

[2022-09-29 04:10:51] local.INFO:  getTempFile $url::  
[2022-09-29 04:10:51] local.INFO: https://picsum.photos/800/600?random=86375  
[2022-09-29 04:10:51] local.ERROR: fopen(https://picsum.photos/800/600?random=86375): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
 {"userId":7,"exception":"[object] (ErrorException(code: 0): fopen(https://picsum.photos/800/600?random=86375): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

When I copypaste code from https://picsum.photos/800/600?random=86375 into browser I got valid image. Is it some protection on side of Spatie\MediaLibrary ?

Thanks in advance!

0 likes
6 replies
PetroGromovo's avatar

@bobbybouwmann It does not work. I got errors :

File `http://local-site.com/Init/hostel_services.jpg` does not exist
File `https://via.placeholder.com/800x600.png/0033ee?text=aspernatur` does not exist

On imige from local site http://local-site.com and absolute path Actually public_path returns PATH of the project - so it can not be used for 2nd url from via.placeholder.com

Niush's avatar
Niush
Best Answer
Level 50

Both, Picsum and Via.Placeholder seems to block requests that has no user-agent header.

If you modify the DefaultDownloader.php with this, it should work.

$context = stream_context_create(
    array(
        "http" => array(
            "header" => "User-Agent: my-demo-laravel-app"
        )
    )
);
fopen("https://picsum.photos/800/600?random=86375", "r", false, $context);

Meanwhile, in your case, instead of using faker provider, you could use unsplash. It does not seem to block fopen requests like the others.

$this->modelItem->addMediaFromUrl('https://source.unsplash.com/random/400x400')

Someone could make a pull request to spatie media-library to add custom headers if needed.

1 like
AMubarak's avatar

@Niush

unsplash did not work for me got the same error which is "cannot be reached spatie", instead use loremflickr

$yourModel->addMediaFromUrl('https://loremflickr.com/320/240')->toMediaCollection('yourCollection');
2 likes

Please or to participate in this conversation.