flysystem icon indicating copy to clipboard operation
flysystem copied to clipboard

V2: Moving and Copying do not overwrite target location

Open fisharebest opened this issue 3 years ago • 9 comments

Not sure if this a documentation error or a bug.

According to https://flysystem.thephpleague.com/v2/docs/usage/filesystem-api/

Moving and copying are both deterministic operations. This means they will always overwrite the target location

The function move() can move both files and folders. The term "target location" (as opposed to "target file") implies both files and folders.

However, the target is overwritten only when the source and destination are both files. e.g.

php > require 'vendor/autoload.php';
php > $a = new League\Flysystem\Local\LocalFilesystemAdapter('tmp');
php > $f = new League\Flysystem\Filesystem($a);
php > $f->write('foo', 'FOO');
php > $f->createDirectory('bar');
php > $f->move('foo', 'bar'); // I expect 'bar' to be overwritten

Warning: Uncaught League\Flysystem\UnableToMoveFile: Unable to move file from tmp/foo to tmp/bar

If this is a documentation error, then I would need to call delete($destination) or deleteDirectory($destination) first.

However there is no direct way to test if $destination is a file or directory. I could listContents() on the parent directory, filter by filename, then call isDir() or isFile() on the attributes object - but that seems somewhat inefficient.

fisharebest avatar Dec 29 '20 11:12 fisharebest

When testing this in bash, then foo will be moved inside bar. So Im not sure that I would expect bar to be overwritten as you say.

Nyholm avatar Jan 03 '21 21:01 Nyholm

Im not sure that I would expect bar to be overwritten as you say.

I expect it because the documentation says explicitly "… will always overwrite the target …".

So either the code or the documenation should be changed.

Then, if it is a documentation error, I need some way to manually delete the target first - which means I need to know whether the existing target is a file or a directory.

FYI - my use-case is an image library, where photo.jpg could either be a file or it could be a folder containing various cropped/scaled versions of the original file. Hence my copy/move/upload operations need to delete any existing file or folder first

fisharebest avatar Jan 04 '21 07:01 fisharebest

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 02 '21 17:06 stale[bot]

@stalebot - please keep this issue open

fisharebest avatar Jun 03 '21 09:06 fisharebest

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 02 '22 11:03 stale[bot]

@stalebot - this issue still exists in version 3 - please keep it open.

fisharebest avatar Mar 02 '22 11:03 fisharebest

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 01 '22 12:05 stale[bot]

@stalebot - this issue still exists in the latest code - please keep it open.

fisharebest avatar May 01 '22 18:05 fisharebest

We can confirm that the issue exists as described for the local adapter AND for the FTP adapter.

rename() returns false, if the directory to be renamed to already exists: https://github.com/thephpleague/flysystem/blob/3.x/src/Local/LocalFilesystemAdapter.php#L266

jremmurd avatar Jul 29 '22 13:07 jremmurd