$files->copy() method

Copy all files recursively from one directory ($src) to another directory ($dst)

Unlike PHP's copy() function, this method performs a recursive copy by default, ensuring that all files and directories in the source ($src) directory are duplicated in the destination ($dst) directory.

This method can also be used to copy single files. If a file is specified for $src, and only a path is specified for $dst, then the original filename will be retained in $dst.

Example

// Copy everything from /site/assets/cache/foo/ to /site/assets/cache/bar/
$copyFrom = $config->paths->cache . "foo/";
$copyTo = $config->paths->cache . "bar/";
$files->copy($copyFrom, $copyTo); 

Usage

// basic usage
$bool = $files->copy(string $src, string $dst);

// usage with all arguments
$bool = $files->copy(string $src, string $dst, $options = []);

Arguments

NameType(s)Description
srcstring

Path to copy files from, or filename to copy.

dststring

Path (or filename) to copy file(s) to. Directory is created if it doesn't already exist.

options (optional)bool, array

Array of options:

  • recursive (bool): Whether to copy directories within recursively. (default=true)
  • allowEmptyDirs (boolean): Copy directories even if they are empty? (default=true)
  • limitPath (bool|string|array): Limit copy to within path given here, or true for site assets path. The limitPath option requires core 3.0.118+. (default=false).
  • hidden (bool): Also copy hidden files/directories within given $src directory? (applies only if $src is dir) The hidden option requires core 3.0.180+. (default=true)
  • If a boolean is specified for $options, it is assumed to be the recursive option.

Return value

bool

True on success, false on failure.

Exceptions

Method can throw exceptions on error:

  • WireException - if limitPath option is used and either $src or $dst is not allowed


$files methods and properties

API reference based on ProcessWire core version 3.0.244

Latest news

  • ProcessWire Weekly #558
    In the 558th issue of ProcessWire Weekly we'll check out the blog post that introduces ProcessWire 3.0.244, share some recent ProcessWire highlights, and more. Read on!
    Weekly.pw / 18 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK