Semih ARSLAN
Semih ARSLAN

Reputation: 21

Using putFileAs with an external file Laravel

I'm looking to store an external file using putFileAs however I'm having some trouble.

Is it even possible?

Note: I don't want to put as an alternative. Because I want to specify the folder path.

$image = Storage::putFileAs(
    'images',
    'http://path.to/some/external/image.jpg',
    str_random() . '.jpg'
);

I get error

Call to a member function getRealPath() on string

Edit: I edit my code as

$image = Storage::putFileAs(
    'images',
    file_get_contents('http://path.to/some/external/image.jpg'),
    str_random() . '.jpg'
);

But again I get an error:

Symfony\Component\Debug\Exception\FatalThrowableError  : Call to a member function getRealPath() on string
at /home/vagrant/code/laravelapp/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:222 

    218|     * @return string|false    
    219|     */    
    220|     public function putFileAs($path, $file, $name, $options = [])     
    221|     {  > 
    222|         $stream = fopen($file->getRealPath(), 'r');
    223|    
    224|         // Next, we will format the path of the file and store the file using a stream since
    225|         // they provide better performance than alternatives. Once we write the file this
    226|         // stream will get closed automatically by us so the developer doesn't have to.

Upvotes: 1

Views: 2617

Answers (2)

Quockhanh Pham
Quockhanh Pham

Reputation: 412

Bug on laravel 5.5.

Use Storage::put for contents of file.

$image = Storage::put(
    'images/'.str_random(). '.jpg',
    file_get_contents('http://path.to/some/external/image.jpg')
);

Upvotes: 2

George Hanson
George Hanson

Reputation: 3040

The FilesystemAdapter expects the second parameter to be a file and not a string. You used to be able to use file_get_contents and pass that in depending on your Laravel version. Please try this:

$image = Storage::putFileAs(
    'images',
    file_get_contents('http://path.to/some/external/image.jpg'),
    str_random() . '.jpg'
);

Upvotes: 0

Related Questions