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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With