Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function hashName() on string error when use putFile() to storing a file via url

I'm working on an API that users can send a file url to download it to their specific folder that has a name same as their usernames.

First I added new disk option to filesystem configuration named user that holds specific directory same name as authenticated user username. like this:

    'disks' => [
        'user' => [
            'driver' => 'local',
            'root'   => NULL, //set on the fly after user authentication. (LogAuthenticated.php)
        ],
        'local' => [
            'driver' => 'local',
            'root'   => public_path('files'),
        ]
    ]

When a User authenticate, I creates a directory named same as authenticated username on listener of Illuminate\Auth\Events\Authenticated event and set filesystems.disks.user.root config like this :

    public function handle (Authenticated $event)
    {
        $username = $event->user->username;

        if (!Storage::exists($username)) {
            Storage::makeDirectory($username, 0775);
        }
        Config::set('filesystems.disks.user.root', public_path('files/' . $username));
    }

Now I want to store a file from an external url that user provided. Suppose that file has jpg format and I want to store that in photo directory on user Directory with a unique name. for that I wrote this :

Storage::disk('user')->putFile('photos', fopen($photo, 'r'));

But when I run code got this error:

Call to a member function hashName() on string

I do not know what is this error and why occured. if anyone know please help me.

like image 726
A.B.Developer Avatar asked Feb 16 '17 08:02

A.B.Developer


1 Answers

According to the documentation, you could use:

Storage::disk('user')->putFile('photos', new \Illuminate\Http\File($photo));

If photo is a URL, you could try:

Storage::disk('user')->put('file.jpg', file_get_contents($photo));
like image 141
rap-2-h Avatar answered Oct 15 '22 06:10

rap-2-h