tags:

views:

81

answers:

2

Hi all,

I found this question (here) about copying a file without overwriting

http://stackoverflow.com/questions/226894/how-do-you-copy-a-file-in-php-without-overwriting-an-existing-file

What I need is a php script to copy all files in a folder, there are also some subfolders; so it should be recursive.

I need to transfer it via FTP so I don't know this makes a big difference to the approach.

Thanks a lot in advance!

+1  A: 

Try this, from a comment on the manual page for copy:

function recurse_copy($src,$dst) { 
    $dir = opendir($src); 
    @mkdir($dst); 
    while(false !== ( $file = readdir($dir)) ) { 
        if (( $file != '.' ) && ( $file != '..' )) { 
            if ( is_dir($src . '/' . $file) ) { 
                recurse_copy($src . '/' . $file,$dst . '/' . $file); 
            } 
            else { 
                copy($src . '/' . $file,$dst . '/' . $file); 
            } 
        } 
    } 
    closedir($dir); 
}

Note that this solution will happily overwrite any files that exist in the $dst directory. If you want to avoid that, you could wrap the code in this question into a function, and call that function instead of copy.

I'm not sure what you want to transfer via FTP, if you clarify that I'll be happy to edit my answer.

Dominic Rodger
Very kind, thx for the reply. Will this solution work for FTP?I want to transfer a few folders with SQL database files (pretty big files...).I would run the script on a different server then the server where the databases are on. And the destination is another server.Overwriting should be totally avoided as the files are so big.THanks in advance!
laurens
I'm a bit confused by your comment - sorry if I'm being dense. If you want to fetch the files from the remote folder, then you need to do a bit more work (see `ftp_get` and `ftp_nlist`); if you want to put the files on a remote server (so you're running the backup process from the source server) then see `ftp_put`. But Ed's right - PHP is definitely not the tool for this job.
Dominic Rodger
My bad. Apparently I need to look for a bash solution (?)
laurens
That's probably a better idea than PHP!
Dominic Rodger
A: 

There is a big difference between filesystem copy and FTP copy you could take a look at the PEAR FTP Class