Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Folders in Laravel 5.5

Tags:

laravel-5

Scenario

Im trying to allow users to move folders around in laravel 5.5, but Laravels site does not have example code for moving folders only files. I looked elsewhere and came across a couple other answers on stackoverflow and other places and tried them all.

Laravel move all files directory

https://laracasts.com/discuss/channels/laravel/laravel-cannot-use-movedirectory-and-copydirectory-problem

Laravel, unable to copyDirectory or moveDirectory

When I try to move a folder i get this response in Devtools:

0:"I want to move this /public/Gallery"
1:"To here /public/Images"
2:false

From this code in the backend

$item ['path' => '/public/Gallery']
$path = '/public/Images'
$success = File::moveDirectory($item['path'], $path);
return [
    "I want to move this ".$item['path'],
    "To here $path",
    $success,
];

Earlier i was getting a response that said the file already exists although it didnt but ive not since been able to replicate that.

EDIT

using Storage::move() i was able to replicate the 'already exists error'

exception:"League\Flysystem\FileExistsException"
file:"C:\Users\user.name\Desktop\electron-api\vendor\league\flysystem\src\Filesystem.php"
line:402
message:"File already exists at path: public/Images"

Question

How to move folders around in laravel 5 storage, NOT FILES. Preferably using the Storage Facade.

like image 238
Kenziiee Flavius Avatar asked Nov 26 '25 09:11

Kenziiee Flavius


1 Answers

Feel really stupid was using the Method wrongly, Because i didnt see it explicitly referenced anywhere that you had to include the folder name in both the "move from" and "move to" directories essentially, i've been putting this:

Storage::move('Folder', 'OtherFolder');

But this doesnt work, i need to do it like this

Storage::move('Folder', 'OtherFolder/Folder');

The folder being moved into OtherFolder still needs to be named folder, so the name needs to append at the end, laravel wasnt throwing any specific errors that allowed me to understand what i was doing wrong properly.

Hopefully this helps anyone as daft as me in the future.

like image 179
Kenziiee Flavius Avatar answered Nov 28 '25 17:11

Kenziiee Flavius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!