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

12,200

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));
Share:
12,200
A.B.Developer
Author by

A.B.Developer

I am a software Engineer and interested in Web design and Web Developing

Updated on June 15, 2022

Comments

  • A.B.Developer
    A.B.Developer almost 2 years

    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.

  • A.B.Developer
    A.B.Developer about 7 years
    I tried Storage::disk('user')->putFile('photos', new File($photo)); but in this case got this error : Call to undefined method Illuminate\\Support\\Facades\\File::hashName()
  • A.B.Developer
    A.B.Developer about 7 years
    $photo is a url string. does new File() accept this type of parameter?
  • rap-2-h
    rap-2-h about 7 years
    @A.B.Developer OK, fixed for URL
  • A.B.Developer
    A.B.Developer about 7 years
    Storage::disk('user')->put('file.jpg', file_get_contents($photo)); worked. but : I want laravel assign a uuid name to file . in this case I must to specify a file name in first parameter of put() method. how can I do that ?