Results 1 to 3 of 3

Thread: copy two dirs to ftp

  1. #1
    Join Date
    Jan 2008
    Posts
    42
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default copy two dirs to ftp

    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.

  2. #2
    Join Date
    Jan 2008
    Posts
    42
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    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.

  3. #3
    Join Date
    Jan 2008
    Posts
    42
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    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.

    Code:
    <?
    
    $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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •