Laravel rename a directory using Storage facade

Viewed 224

I'd like to rename a directory with the Storage Facade (S3 object). I've tried using move and rename, both with and without trailing slash, none of these work.

Storage::disk('s3')->move($dir_temp, $dir_final);
Storage::disk('s3')->move($dir_temp.'/', $dir_final.'/');
Storage::disk('s3')->rename($dir_temp, $dir_final);
Storage::disk('s3')->rename($dir_temp.'/', $dir_final.'/');
1 Answers

I did use rename function, and it did work for renaming a directory.

This code worked for renaming directory dir 1 to dir 2:

  Storage::disk('movies')->rename('dir 1', 'dir 2');
    

Now, it depends on what error did you get.

  • make sure, you have rwx rights to the parent directory
  • make sure the source directory exists, and the destination does not
  • make sure the name is valid for a name, for example, windows OS will not allow certain characters in the name
  • make sure you do have permissions to the source directory for that operation
  • be very carefull with slashes, as windows OS has different path slashes than linux.

For the directory separator, to be platform independent, you can use

$DS = DIRECTORY_SEPARATOR;

Also, the permissions can be quite tricky, as, if you have the app deployed, traditionally, the http user is the user under which all the code is executed, so keep that in mind.

Related