- 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 [1126], Last Updated: Sat May 18, 2024
wallpaperama
Sat Mar 08, 2008
26 Comments
13497 Visits
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.phpHow 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)
https://www.wallpaperama.com/forums/how-to-copy-file-or-directory-in-php-scripts-code-copying-files-directories-t5847.html