I want copy all the files of a folder to another folder. So how to copy recursively in wordpress.
1 Answer
You can make use of the Filesystem API
, especially the copy_dir()
function – source – does seem to be what you are looking for. Take a look at the docblock from the above source link for a little bit more information:
/**
* Copies a directory from one location to another via the WordPress Filesystem Abstraction.
* Assumes that WP_Filesystem() has already been called and setup.
*
* @since 2.5.0
*
* @param string $from source directory
* @param string $to destination directory
* @param array $skip_list a list of files/folders to skip copying
* @return mixed WP_Error on failure, True on success.
*/
The (very) basic usage scenario would look like this:
global $wp_filesystem;
copy_dir(
'/path/you/are/copying/from/',
'/path/you/are/copying/to/'
);
Unfortunately the Filesystem API
isn’t exactly well documented. You definitely should take a look at the linked codex article. One additional (almost always) linked source of information would be the article by @Otto:
- Tutorial: Using the WP_Filesystem
Besides that there are some Q&A’s on here which might be helpful:
- Using wp_filesystem in Plugins
- Use WP_Filesystem to list files in directory
- Copy and delete a directory with WordPress functions
Note, the above are just a small sample, use the search to find more. Last but not least, to take a closer look at the source to learn and know more about it should definitely be on your agenda.