Copy entire folder from one storage to another – Laravel

In this tutorial, we are going to show you, how you can copy or move a folder from one storage to another storage folder in Laravel.

Video tutorial

So let’s say in your storage/app/public folder, these folders are already created by Laravel by default. We created 2 folders named folder1 and folder2. In folder1, we have a folder named files and it has some files. Now you want to move the entire files folder with all his files in folder2.

  • storage/app/public
    • folder1
      • files
    • folder2

Copy folder from one storage to another – Laravel

Following will be the code that will copy the entire folder from one storage to another:

// include classes
use Storage;
use Str;

// get all files from source folder
$files = Storage::files("public/folder1/files");

// loop through each file
foreach ($files as $file)
{
    // to get the content the file, we need to get it from public/storage
    // so change the path to public/storage folder
    $file = Str::of($file)->replace("public/folder1/", "public/storage/folder1/");

    // get the content of file
    $file_content = file_get_contents($file);

    // extract the file name from path
    $file_name_parts = explode("/", $file);
    if (count($file_name_parts) > 0)
    {
        // name is at the last index of array
        $file_name = $file_name_parts[count($file_name_parts) - 1];

        // set destination path
        $file_path = "public/folder2/files/" . $file_name;

        // save the file in destination folder
        Storage::put($file_path, $file_content);
    }
}

// delete the source folder
// do this only if you want to move the folder
Storage::deleteDirectory("public/folder1/files");

Explanation

  1. First, we are going to get all the files from the source folder.
  2. Then we will loop through each file.
  3. Then we need to get the content of each file. But in order to get the content of the Laravel storage file, we need to access it from a public shortcut path.
  4. So to change the path, we are going to use another class named Str.
  5. Then we need to call the function of and tell the target string and call the replace function. We will replace the “public/folder1/” (notice slash at the end) with the “public/storage/folder1/”. And save it back in the $file variable.
  6. Now we can get the content of the file by calling the PHP native function file_get_contents and pass the path of the file, and save it in the variable $file_content. If you get any error here, you need to run the following command at the root of your project:
    • php artisan storage:link
  7. Then we need to extract the file name from the file path. First, we will split the file path by slash using PHP explode function. It will return an array and we need to get the last index of this array. So we will check if the array is not empty. Then we will get the last index of the array. So if the length of the array is 5, minus 1 becomes 4, so it will fetch the element at index 4 which is the name of the file.
  8. After that, we are setting the destination path. It will be in the public/folder2/files and the name of the file. Right now, folder2 does not have a files folder, but it will automatically create one. To save the file, we need to call the Storage::put method. Pass the destination path and the content of the file.
  9. In order to have the move functionality, you simply need to remove the source folder after it has been copied. So after the foreach loop, we will call the deleteDirectory method from the Storage class and pass the path of the source folder.
  10. If you run the code now, you will see that folder1 becomes empty but now folder2 has all the files.

If you are working on AJAX and are getting any errors, feel free to check our tutorial on how to check Laravel errors from AJAX requests.

So that’s how you can copy or move an entire folder from one storage to another in Laravel. If you face any problems in following this, kindly do let me know.