dan3460's avatar

Moving files to public directory

I'm a little confused on the way the Storage::move(), and copy or delete, work. My original command was: The content of $file = 'temp/arcash.cvs'

$movedTo = Str::replaceFirst('temp/', '', $file);
Storage::move($file, public_path($movedTo));
...

but i'm getting the following error:

League/Flysystem/Exception with message 'Impossible to create the root directory "C:/Users/Daniel.SFP/Documents/Pr
ojects/Credit Limit/Armanager/storage/app/C:/Users/Daniel.SFP/Documents/Projects/Credit Limit/Armanager/public". '

I can probably hard code the path to the public folder, but i'm puzzled as to why this happens. Could someone explain please.

0 likes
3 replies
bobbybouwmann's avatar

Moving files, or in general working with the Storage class is based on the path of the configured storage driver. So if you have the local driver, by default it already points to the correct directory. So the only you need to do is using the relative path instead of the absolute path.

In this case, public_path returns the full absolute pathname, but you only need the relative one here.

dan3460's avatar

I have another problem. I'm moving the files to the public storage, how i can force to overwrite the files, if they are there?

bobbybouwmann's avatar

You need to delete the previous file first before you can move it

if (Storage::exists($file)) {
    Storage::delete($file);
}

Storage::move('uploads/'.$filename, $file);

Please or to participate in this conversation.