Page 1 of 1
Can you use PHP to copy Folder from one host to another?
Posted: Wed Oct 31, 2012 5:54 am
by simonmlewis
Code: Select all
<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
This works for copying a file from one host to another.
But is there a similar fairly simple script, that copies an entire folder, from one to another?
I didn't write this, so I am not sure if this can be modified easily to do such a thing?
Re: Can you use PHP to copy Folder from one host to another?
Posted: Wed Oct 31, 2012 12:19 pm
by requinix
Define "fairly simple".
Code: Select all
function ftp_put_recursive($conn_id, $remote_path, $path) {
if $path is a directory {
for each $thing in $path {
// assuming $thing is the full path, not just a name
ftp_put_recursive($conn_id, $remote_path . "/" . basename($thing), $thing);
}
} else if $path is a file {
ftp_put($conn_id, $remote_path, $path);
} // else $path doesn't exist so don't do anything
}
Re: Can you use PHP to copy Folder from one host to another?
Posted: Fri Nov 02, 2012 3:00 pm
by Tiancris
In a web application I'm working on, we have a cron executing a php script. This cron is running as root user and the script is out of public access, so we have enough privileges to copy files from a web site to another doing the following:
Code: Select all
$from = '/home/site1/source_dir/.';
$to = '/home/site2/destination_dir/';
exec("cp -R $from $to");
Nice, isnt it?
