How to fix undefined method File::hashName() in Laravel Storage putFile()
In an app that I’m currently working on, I need to copy some large files from local disk over to DigitalOcean Spaces everyday after processing. Because the files are quite large, I need to do it via streaming instead or I will exhaust my VPS’s memory.
Following Laravel’s documentation, I have some codes that look like this:
$path = \Storage::disk('local')->path('data.csv');$file = new \File($path);\Storage::disk('digitalocean')->putFile('archive', $file);
But I would get an error like this and I have no idea why!
Call to undefined method Illuminate\Support\Facades\File::hashName() at vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:259
If I change to use
putFileAs() instead, I would get this error instead:
Call to undefined method Illuminate\Support\Facades\File::getRealPath() at vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:273
I’m pulling my hair at this point because WHY DOESN’T IT WORK! It makes no sense!
Turns out that I didn’t follow the documentation exactly.
How to fix it?
You see when I see the sample codes from documentation as such:
$path = Storage::putFile('photos', new File('/path/to/photo'));
I automatically assume that this
File comes from
Illuminate\Support\Facades\File , which can be conveniently accessed via
File actually comes from
Illuminate\Http\File ! In fact it was clearly written above the line of codes I copied from in the documentation… My bad for missing it entirely.
So to get my codes working, all I have to do is import this class and update the
use Illuminate\Http\File;$path = \Storage::disk('local')->path('data.csv');$file = new File($path);\Storage::disk('digitalocean')->putFile('archive', $file);
Tada~ It finally works now.