Copy a directory recursively in php (using Standard PHP Library)

guttume • October 20, 2019

Last time, we discussed how to remove a non empty directory in php using Standard PHP Library (SPL). This time we will learn how to copy a directory recursively in php using SPL. By recursively, I mean we want to copy a directory which has directories inside it along with files and those directories can have more directories inside them.

This time we will use another SPL class: The DirectoryIterator class. As per the documentation

The DirectoryIterator class provides a simple interface for viewing the contents of filesystem directories.

In simple words, it lets us iterate through the files in a given directory while providing methods to get several information about those file. We will use couple of those methods in our function but I do encourage you to look at the documentation for the full list of methods this class provides.

Before we get to the code, we are going to discuss, how we will approach our problem. In order to copy one directory to another with its contents, we need to copy files from our source directory to the target directory. Lets break it down into steps: