Laravel 重命名文件

Laravel rename file

我正在尝试重命名 laravel 5.6

中的文件

当我对文件进行物理重命名时,这工作正常,但当我只是改变大小写时 - 它会引发错误:

League\Flysystem\FileExistsException: File already exists at path

例子

old path: Music/The Corrs/The Corrs - What Can I Do.mp3
new path (DO): Music/The Corrs/The Corrs - What Can I DO.mp3

代码

Storage::disk($disk)->move($old, new);

我该如何解决这个问题,以便在情况发生变化时可以 rename/move 文件?

在 Windows 上,文件名不区分大小写。所以名称为uppercase.txtUPPERCASE.txt的文件是相同的,但是一个文件夹中不可能有两个同名的文件。
重命名时,文件实际上是 "moved" 为另一个名称。但是文件已经存在,所以你得到一个错误。

我建议先将文件存储在临时文件夹中以重命名或在重命名之前为文件使用临时名称。

现成的解决方案

$oldPath = "Music/The Corrs/The Corrs - What Can I Do.mp3";
$newPath = "Music/The Corrs/The Corrs - What Can I DO.mp3";
$paddedPath = str_replace(" ", "@@padding@@", $oldPath);

//move from old path to padded path
Storage::move(
   $oldPath, 
   $paddedPath, 

);

//move from padded path to new
Storage::move(
    $paddedPath, 
    $newPath
);