Copy all files and folder from one directory to another directory PHP

Posted by santosh on Stack Overflow See other posts from Stack Overflow or by santosh
Published on 2009-10-03T12:29:15Z Indexed on 2010/04/16 9:03 UTC
Read the original article Hit count: 285

Filed under:
|

Hi,

I have directory called "mysourcedir" it has sonme files and folders. so i want to copy all content from this directory to some other "destinationfolder" on Linux server using PHP.

function full_copy( $source, $target ) {
if ( is_dir( $source ) ) {
	@mkdir( $target );
	$d = dir( $source );
	while ( FALSE !== ( $entry = $d->read() ) ) {
		if ( $entry == '.' || $entry == '..' ) {
			continue;
		}
		$Entry = $source . '/' . $entry; 
		if ( is_dir( $Entry ) ) {
			$this->full_copy( $Entry, $target . '/' . $entry );
			continue;
		}
		copy( $Entry, $target . '/' . $entry );
	}

	$d->close();
}else {
	copy( $source, $target );
}

}

I am trying this code, but it does some problem, it creates directory "mysourcedir" at destination location. I am expecting to just copy all files and folders at destination,. Please suggest

© Stack Overflow or respective owner

Related posts about php

Related posts about linux