The copy_dir() WordPress PHP function is used to copy a directory from one location to another utilizing the WordPress Filesystem Abstraction. This function assumes that WP_Filesystem() has already been set up.
Usage
To use the copy_dir() function, you will need to provide the source directory, the destination directory, and optionally, an array of files or folders that you wish to exclude from the copying process. Here’s an example:
global $wp_filesystem; // Ensure the target directory exists $wp_filesystem->mkdir( $target_dir ); // Use copy_dir to copy files from source to target copy_dir( $src_dir, $target_dir );
Parameters
- $from (string): This is the source directory from which files are copied.
- $to (string): This is the destination directory where files are copied to.
- $skip_list (string): Optional. An array of files or folders that you don’t want to copy. Default is an empty array.
More Information
See WordPress Developer Resources: copy_dir()
This function is a part of WordPress core and is implemented in WP_Filesystem().
Examples
Basic Usage
This example demonstrates how to use the copy_dir() function to copy files from one directory to another.
global $wp_filesystem; // Set up source and target directories $src_dir = '/path/to/source'; $target_dir = '/path/to/target'; // Ensure target directory exists $wp_filesystem->mkdir( $target_dir ); // Copy files copy_dir( $src_dir, $target_dir );
Skipping Specific Files
This example shows how to skip specific files or folders when copying.
global $wp_filesystem; // Set up source and target directories $src_dir = '/path/to/source'; $target_dir = '/path/to/target'; // Files/folders to skip $skip_list = array('file1.txt', 'folder1'); // Ensure target directory exists $wp_filesystem->mkdir( $target_dir ); // Copy files, skipping specified ones copy_dir( $src_dir, $target_dir, $skip_list );
Using Paths from Variables
This example demonstrates using copy_dir() with paths stored in variables.
global $wp_filesystem; // Paths stored in variables $src_dir = $sourcePath; $target_dir = $destinationPath; // Ensure target directory exists $wp_filesystem->mkdir( $target_dir ); // Copy files copy_dir( $src_dir, $target_dir );
Copying Nested Directories
This example demonstrates copying a directory along with its nested directories.
global $wp_filesystem; // Set up source and target directories $src_dir = '/path/to/source/with/nested/dirs'; $target_dir = '/path/to/target'; // Ensure target directory exists $wp_filesystem->mkdir( $target_dir ); // Copy files and nested directories copy_dir( $src_dir, $target_dir );
Checking If Copy Was Successful
This example demonstrates how to check if the directory copy operation was successful.
global $wp_filesystem; // Set up source and target directories $src_dir = '/path/to/source'; $target_dir = '/path/to/target'; // Ensure target directory exists $wp_filesystem->mkdir( $target_dir ); // Copy files if(copy_dir( $src_dir, $target_dir )) { echo "Copy successful!"; } else { echo "Copy failed."; }
In this example, the function copy_dir()
returns true if the copy operation is successful and false otherwise. We check the return value in an if
statement and output a message accordingly.