首页 > 解决方案 > 将 putFileAs 与外部文件 Laravel 一起使用

问题描述

我正在寻找使用存储外部文件的方法,putFileAs但是遇到了一些麻烦。

甚至可能吗?

注意:我不想put作为替代方案。因为我想指定文件夹路径。

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

我收到错误

调用字符串上的成员函数 getRealPath()

编辑:我将我的代码编辑为

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

但我又得到一个错误:

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.

标签: laravel

解决方案


laravel 5.5上的错误。

使用Storage::put作为文件内容。

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

推荐阅读