Wallpapers Forums Login

How To Copy File Or Directory In Php Scripts Code Copying Files Directories

today i was wondering if it would be possible to copy directories or file using php code..

well, you're in luck because yes it is possible.


How To Copy File



Example Script CODE:
<?php
$file = 'old_file.txt';
$newfile = 'new_file.txt';

if (!copy($file, $newfile)) {
echo "failed to copy $file...n";
}
?>


you can get more info at:
http://www.php.net/manual/en/function.copy.php

How to copy Directory

to copy directory you can use this handy function:

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 ) ) {
				full_copy( $Entry, $target . '/' . $entry );
				continue;
			}
			copy( $Entry, $target . '/' . $entry );
		}
 
		$d->close();
	}else {
		copy( $source, $target );
	}
}


you can call this function with this example, lets say i want to copy my themes from my source files to my new websites i would do this:

PHP CODE
$source ='/var/www/source/theme';
$destination = '/var/www/newsite/theme/';
full_copy($source, $destination)
Rakesh Sun Apr 21, 2013
Very very very very thank you
I was distraught for one week ,
I got Solution On your website.
Please so how to implement as a tutorial.

Thank you so much.......
Solomon Mon May 28, 2012
Thanks boss, much love for the code....really helped
louie Sun May 20, 2012
thanks a lot..
VINAYSINGH Thu Mar 22, 2012
nice CODE VERY GOOD TO HELPS US
gopal Mon Jan 30, 2012
thnaks a lot
this code is very useful for me
Rahul Sun Jan 08, 2012
Thanks
i m seeking it From a long time
Thanks
Christopher Fri Dec 30, 2011
Thanks! This did what I needed it to do.
Rehan Mon Oct 17, 2011
its really helpful,
i will try this on Site..
jony Wed Oct 12, 2011
Really awesomee.........Thnks a lot...
Victor Wed Oct 12, 2011
Will it copy the contents too?
Hammad Fri Aug 12, 2011
Thanks alot Sir...
This Code is doing work perfectly..
Thanks again..
Sumit Wed Jun 15, 2011
Great Post Friend.

You solved the problem. And the solutions is great.
John Tue May 24, 2011
Just what I needed! Works flawlessly.
Thank you so much.
ico Sat Apr 30, 2011
this is great!!!
Muhammad Thu Apr 28, 2011
You have done a good job.

Related Content

Information

Share

wallpaperama | Wallpapers | Forums | Terms Of Service

copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Fri May 24, 2013 (-8 GMT)
Powered by: Webune Forums V5