Wednesday 16 April 2014

PHP: How to Copy All Files and folders Within A Directory

function recurse_copy($src,$dst) {
        $dir = opendir($src);
        @mkdir($dst, 0777);
        while(false !== ( $file = readdir($dir)) ) {
                if (( $file != '.' ) && ( $file != '..' )) {
                        if ( is_dir($src . '/' . $file) ) {
                                $this->recurse_copy($src . '/' . $file,$dst . '/' . $file);
                        }
                        else {
                                copy($src . '/' . $file,$dst . '/' . $file);
                        }
                }
        }
        closedir($dir);
    }

Example : How to use above Function

$src='www/abc'; // copy all the files and folder within abc directory
$dst = 'www/newdir' //
recurse_copy($src,$dst);

above function first create the destination directory and than copy all the source directory content.
Tips: to create directory and set its permission using following code in php
mkdir($path, 0777, true)

No comments:

Post a Comment

List OF BANK PAN Numbers

List OF BANK PAN Numbers Bank/Home Loan Providers PAN Number Allahabad Bank AACCA8464F Andhra Bank AABCA7375C Axis Bank...