Home > Backend Development > PHP Tutorial > How to Copy a Directory\'s Contents Using PHP Recursively?

How to Copy a Directory\'s Contents Using PHP Recursively?

Patricia Arquette
Release: 2024-10-29 10:30:29
Original
1111 people have browsed it

How to Copy a Directory's Contents Using PHP Recursively?

Copying a Directory's Contents using PHP

When attempting to copy the contents of a directory to another location, the code copy ("old_location/*.*","new_location/") may encounter an error indicating that the stream is not found. This can occur when an asterisk (*) is used alone in the copy function.

To effectively copy the entire contents of a directory, including its subdirectories and files, we can employ a recursive function like the one below:

<code class="php">function recurseCopy(
    string $sourceDirectory,
    string $destinationDirectory,
    string $childFolder = ''
): void {
    // Open the source directory
    $directory = opendir($sourceDirectory);

    // Check if the destination directory exists, if not, create it
    if (is_dir($destinationDirectory) === false) {
        mkdir($destinationDirectory);
    }

    // If there's a child folder specified, create it if it doesn't exist
    if ($childFolder !== '') {
        if (is_dir("$destinationDirectory/$childFolder") === false) {
            mkdir("$destinationDirectory/$childFolder");
        }

        // Loop through the files in the source directory
        while (($file = readdir($directory)) !== false) {
            // Ignore current and parent directories
            if ($file === '.' || $file === '..') {
                continue;
            }

            // If the current file is a directory, recursively copy it
            if (is_dir("$sourceDirectory/$file") === true) {
                recurseCopy("$sourceDirectory/$file", "$destinationDirectory/$childFolder/$file");
            }
            // If it's a file, simply copy it
            else {
                copy("$sourceDirectory/$file", "$destinationDirectory/$childFolder/$file");
            }
        }

        // Close the source directory
        closedir($directory);

        // Recursion ends here
        return;
    }

    // Loop through the files in the source directory
    while (($file = readdir($directory)) !== false) {
        // Ignore current and parent directories
        if ($file === '.' || $file === '..') {
            continue;
        }

        // If the current file is a directory, recursively copy it
        if (is_dir("$sourceDirectory/$file") === true) {
            recurseCopy("$sourceDirectory/$file", "$destinationDirectory/$file");
        }
        // If it's a file, simply copy it
        else {
            copy("$sourceDirectory/$file", "$destinationDirectory/$file");
        }
    }

    // Close the source directory
    closedir($directory);
}</code>
Copy after login

By using this recursive function, we can efficiently copy the entire contents of a directory to another location, ensuring that all subdirectories and files are properly transferred.

The above is the detailed content of How to Copy a Directory's Contents Using PHP Recursively?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template