Asked 7 years ago
29 Dec 2016
Views 938

posted

how to Copy a directory from one location to another in php

how to copy directory and its inner files , from one location to another in php , any lib or code to do copy from one location to another whole folder easily

ruby-rails

ruby-rails
answered Apr 24 '23 00:00

To copy a directory from one location to another in PHP without using any external libraries, you can use a combination of built-in functions such as is_dir(), mkdir(), opendir(), readdir(), closedir() , and file_put_contents().

Here's an example code snippet to copy a directory in PHP:



$src = '/path/to/source/directory';
$dst = '/path/to/destination/directory';

if (is_dir($src)) {
    if (!is_dir($dst)) {
        mkdir($dst);
    }

    $dir = opendir($src);
    while (false !== ($file = readdir($dir))) {
        if ($file == '.' || $file == '..') {
            continue;
        }

        $srcFile = $src . '/' . $file;
        $dstFile = $dst . '/' . $file;

        if (is_dir($srcFile)) {
            copyDirectory($srcFile, $dstFile);
        } else {
            file_put_contents($dstFile, file_get_contents($srcFile));
        }
    }

    closedir($dir);
    echo "Directory copied successfully.";
} else {
    echo "Source directory does not exist.";
}

function copyDirectory($src, $dst) {
    if (!is_dir($dst)) {
        mkdir($dst);
    }

    $dir = opendir($src);
    while (false !== ($file = readdir($dir))) {
        if ($file == '.' || $file == '..') {
            continue;
        }

        $srcFile = $src . '/' . $file;
        $dstFile = $dst . '/' . $file;

        if (is_dir($srcFile)) {
            copyDirectory($srcFile, $dstFile);
        } else {
            file_put_contents($dstFile, file_get_contents($srcFile));
        }
    }

    closedir($dir);
}

In this example, we first check if the source directory exists and if the destination directory does not exist, we create it using the mkdir () function.

We then open the source directory using opendir () function and loop through each file and subdirectory using readdir () function. We use continue statement to skip the . and .. directories.

Next, we create the full path for the source and destination files using concatenation of the file name with the source and destination directory paths. If the file is a directory, we recursively call the copyDirectory () function passing the source and destination directory paths as arguments. If the file is not a directory, we copy its contents to the destination file using the file_put_contents( ) function.

Finally, we close the directory using closedir() function.

The copyDirectory( ) function is defined to handle the recursive copying of directories. It creates the destination directory using the mkdir () function and then follows the same logic as the main function for copying the files and subdirectories.

This code will copy the entire source directory to the destination directory, including all subdirectories and files.
Post Answer