php

Copying a Directory From One Location to Another

Problem

You want to copy an entire directory to another location.

You want to copy it recursively, all files and subdirectories, to the new location.

Solution

Use the File::copyDirectory() method.

$success = File::copyDirectory($sourceDir, $destinationDir);

The method will return true if all files and subdirectories are successfully copied.

If the destination directory doesn’t exist it will be created. It will be created recursively as needed.

Discussion

There’s an optional third argument.

Internally, the File::copyDirectory() method uses PHP’s FilesystemIterator class to scan the files and directories to copy. The FilesystemIterator takes flags as a second parameters. You can pass a third argument to File::copyDirectory() which gets passed along to the FilesystemIterator constructor. By default, File::copyDirectory() uses the SKIP_DOTS constant.

This means files beginning with dots are not copied.

Be careful using this third option. The File::copyDirectory() method may not behave as expected unless you understand well how the FilesystemIterator is used for directory traversal.

About the author

laravelrecipies