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

Forums » PHP » How To Copy File Or Directory In Php Scripts Code Copying Files Directories
in this post i will teach you how to copy a file or a directory using PHP with a sample script and will give you the code you can use for copying files and 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)
wallpaperama
this was the best i can do i hope it works for you
wallpaperama
if it doesnt work for you let me know
Marco
Thank you for this great work!

It perfectly matches what I'm looking for and is made in the programming style I like.

It saved me a lot of time.
Sanjeev
Superb Site Thanks a lot for save my time.
anju
Thanks a lot Sir, for your code. It help me a lot. save my valuable time
wayes
Great Job! Really saved my time!! Thanks a lot.
ravic
hi this is mr ravi from hyderabad. i am a php developer. i would like to tell very very thanks coz i was searching code for copying files from folder to another but i didn't found any where until i was entered into this site very very thanks. not only this but also exporting excel sheet.
Arun
Great work. Thanks! You saved my time. I was looking for the exact code part for one of my current projects.
Abhishek
Great solution to a common problem..
Fran
Thank you. You saved my life. ;-) Great work. - at - F.
Muhammad
You have done a good job.
ico
this is great!!!
John
Just what I needed! Works flawlessly. Thank you so much.
Sumit
Great Post Friend. You solved the problem. And the solutions is great.
Hammad
Thanks alot Sir... This Code is doing work perfectly.. Thanks again..
Victor
Will it copy the contents too?
jony
Really awesomee.........Thnks a lot...
Rehan
its really helpful,
i will try this on Site..
Christopher
Thanks! This did what I needed it to do.
Rahul
Thanks i m seeking it From a long time Thanks
gopal
thnaks a lot this code is very useful for me
VINAYSINGH
nice CODE VERY GOOD TO HELPS US
louie
thanks a lot..
Solomon
Thanks boss, much love for the code....really helped
Rakesh
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.......
Title: How To Copy File Or Directory In Php Scripts Code Copying Files Directories [1126]
Tags: php copy file directory script code tutorial howto
By , 26 Comments