Page 1 of 1

Copy files and folder in a new folder

Posted: Fri Mar 13, 2009 1:42 am
by shafiq2626
Helow!
I have a folder which contain many files and folders.
i want to create a new folder through php script and past all the files and sub folderes and the file of sub folder from my exiting folder in my new folder

pls tell me how i can copy the folder like images,admin etc and its files


pls help me as soon as possible
thanks :banghead:

Re: Copy files and folder in a new folder

Posted: Sat Mar 14, 2009 12:54 am
by shafiq2626
this problem is solved by this script

<?php
/**
* Copies a Directory Contents to new Directory
*/
class copyDir {

var $dir1;
var $dir2;
var $errtxt;
var $overwrite_files;
var $recurse;
var $copy_callback;
var $copy;

function copyDir() {
$this->overwrite_files = false;
$this->recursive = false;
$this->copy_callback = false;
$this->copy = new stdClass();
}

/**
* Set the directory to copy from
*/
function setCopyFromDir( $dir ) {
if (!is_dir($dir)) {
$this->_error('The supplied argument, '.$dir.', is not a valid directory path!');
return false;
}
$this->dir1 = $dir;
return true;
}

/**
* Set the directory to copy to
*/
function setCopyToDir( $dir ) {
if (!is_dir($dir)) {
if (!mkdir($dir)) {
$this->_error('Could not create directory, '.$dir);
return false;
}
}
$this->dir2 = $dir;
return true;
}

/**
* Set if we want to copy sub folders or not
* @param bool TRUE or FALSE
*/
function copySubFolders( $bool = true ) {
$this->recurse = $bool;
}

/**
* Set if we want to overwrite existing files or not
* @param bool TRUE or FALSE
*/
function overWriteFiles( $bool = true ) {
$this->overwrite_files = $bool;
}

/**
* Set a callback function to be executed each time a file or sub folder is copied
* @param string callback function name
*/
function setCopyCallback( $fn ) {
$this->copy_callback = $fn;
}

/**
* Create the directory copy
* @param bool Recurse through sub directories?
*/
function createCopy() {
if ( !is_dir($this->dir1) ) {
$this->_error('Directory to copy from is not a directory. Set the directory using setCopyFromDir()');
return false;
}
if ( !is_dir($this->dir2) ) {
$this->_error('Directory to copy to is not a directory. Set the directory using setCopyToDir()');
return false;
}

// all set, start copy
if ($this->_makeCopy($this->dir1, $this->dir2, $recurse)) {
return true;
}
return false;
}

function _makeCopy($dir1, $dir2) {
if ($dh = opendir($dir1)) {
$i = 0;
$dirArr = array();
while ($el = readdir($dh)) {
$path1 = $dir1.'/'.$el;
$path2 = $dir2.'/'.$el;
$this->_setCopyStatus($path1, $path2);

if (is_dir($path1) && $el != '.' && $el != '..') {
if (is_dir($path2)) {
continue;
}
if (!mkdir($path2)) {
$this->_error('Could not create new directory, '.$path2);
return false;
}
if ($this->recurse) { // copy sub directories recursively
_makeCopy($path1, $path2);
}
} elseif (is_file($path1)) {
if (is_file($path2) && !$this->overwrite_files) {
$this->_error("Duplicate File Exists, when trying to copy file: $path1, to $path2.<br /> Set overWriteFiles() if you need to overwrite existing files.");
return false;
}
if (!copy($path1, $path2)) {
$this->_error('Could not copy file, '.$path1.', to '.$path2);
return false;
}
}
$i++;
}
closedir($dh);
return true;
} else {
$this->_error('Could not open the directory, '.$dir1);
}
return false;
}

function _setCopyStatus($path1, $path2) {
$this->copy->oldfile = $path1;
$this->copy->newfile = $path2;
$this->_callback();
}

function _callback() {
if ($this->copy_callback) {
call_user_func($this->copy_callback, $this->copy);
}
}

function _error($txt) {
$this->errtxt = $txt;
}

/**
* View the last error logged
*/
function viewError() {
return $this->errtxt;
}

}
/**
* Usage of the copy directory class
*/

$dir = dirname(__FILE__); // the directory this script is in
$cp = new copyDir();

// set the directory to copy
if (!$cp->setCopyFromDir( $dir.'/'.$_POST['template'].'/' )) {
echo $cp->viewError();
die;
}

// set the directory to copy to

if (!$cp->setCopyToDir( $dir.'/'.$_POST['webname'].'/' )) {
echo $cp->viewError();
die;
}


if (!$cp->setCopyFromDir( $dir.'/'.$_POST['template'].'/images' )) {
echo $cp->viewError();
die;
}

// set the directory to copy to

if (!$cp->setCopyToDir( $dir.'/'.$_POST['webname'].'/images' )) {
echo $cp->viewError();
die;
}

$cp->copySubFolders(true); // include sub folders when copying
//$cp->overWriteFiles(true); // overwrite existing files

$cp->setCopyCallback( 'updateCopyProgress' );

if (!$cp->createCopy( true )) { // create a copy and recurse through sub folders
echo $cp->viewError();
die;
}


if (!$cp->setCopyFromDir( $dir.'/'.$_POST['template'].'/SpryAssets' )) {
echo $cp->viewError();
die;
}

// set the directory to copy to

if (!$cp->setCopyToDir( $dir.'/'.$_POST['webname'].'/SpryAssets' )) {
echo $cp->viewError();
die;
}

$cp->copySubFolders(true); // include sub folders when copying
//$cp->overWriteFiles(true); // overwrite existing files

$cp->setCopyCallback( 'updateCopyProgress' );

if (!$cp->createCopy( true )) { // create a copy and recurse through sub folders
echo $cp->viewError();
die;
}

if (!$cp->setCopyFromDir( $dir.'/'.$_POST['template'].'/inc' )) {
echo $cp->viewError();
die;
}

// set the directory to copy to

if (!$cp->setCopyToDir( $dir.'/'.$_POST['webname'].'/inc' )) {
echo $cp->viewError();
die;
}

$cp->copySubFolders(true); // include sub folders when copying
//$cp->overWriteFiles(true); // overwrite existing files

$cp->setCopyCallback( 'updateCopyProgress' );

if (!$cp->createCopy( true )) { // create a copy and recurse through sub folders
echo $cp->viewError();
die;
}


// this is our custom function which is called each time we are about to make a new file copy
// or create a new sub directory
function updateCopyProgress(&$copy) {
$file1 = $copy->oldfile;
$file2 = $copy->newfile;
$type = is_dir($file1) ? 'dir' : 'file';

if ($type == 'dir') {
//echo "Creating new Directory, $file2<br />";
} else {
// echo "Copying $file1 to $file2<br />";
}
flush(); // send this to the browser

}
?>

:mrgreen: 8O

Re: Copy files and folder in a new folder

Posted: Sat Mar 14, 2009 1:23 am
by Benjamin
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]