View Full Version : copy two dirs to ftp
jfreak53
06-16-2009, 05:56 PM
I wonder if someone could write a short script for me, I'm not that good with FTP commands. I need a short PHP script that will read the directory list of two local directories from my server, then copy each file in the directory to the equal directory on the FTP server.
Thanks in advance.
jfreak53
06-16-2009, 09:25 PM
No sorry I didn't explain better, I am not looking for user control, I have a php system that runs directory controls local and on another server. It's not users I just need to copy files that a php system created to an ftp server. Not users.
jfreak53
06-17-2009, 01:58 PM
Ok, After much trial and error and finding a script that was similar but did it all in reverse I have made one that does exactly what I want. This is how to upload a whole, or in my case two whole dirs to an FTP. There is another part that does directories, but since I won't have any dirs I got rid of it.
<?
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = "";
$ftp_dir = "/images1/";
$ftp_dir2 = "/thumbs1/";
$dirLocal = "C:\\xampp\\htdocs\\images\\";
$dirLocal2 = "C:\\xampp\\htdocs\\thumbs\\";
// 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);
echo "Starting ".time()."<br>";
transferFiles($ftp_dir, $dirLocal);
transferFiles($ftp_dir2, $dirLocal2);
function transferFiles($dirRemote, $dirLocal){
global $conn_id;
if ($conn_id != false){
// get contents of the current directory
$contents = scandir($dirLocal);
foreach($contents as $file){
if (filesize($dirLocal.$file) > 0) {
echo "<i>$dirLocal$file</i><br>";
//If File Exists Skip.
if (ftp_size($conn_id,$dirRemote.$file) == filesize($dirLocal.$file)){
echo "- skipped <br>";
}
else {
ftp_put($conn_id, $dirRemote.$file, $dirLocal.$file, FTP_BINARY);
unlink($dirLocal.$file);
echo "- completed <br>";
}
}
}
}
}
?>
I hope this serves someones use sometime in the future.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.