Welcome Guest, Not a member yet? Register   Sign In
Copy Files from the Server using FTP in php
#3

[eluser]magz[/eluser]
@xwero, tnx for the hint.. :-)

it somehow lead me to the way.. I've got what I want.
Can you check my codes?Is there anyway to make it more optimized?,
not that so important, anyway, it already working. just want to
know if there are better way..

check code below:

Code:
<?php

$ftp_server    = "example.com";
$ftp_user_name = "username";
$ftp_user_pass = "password";

$local_dir = 'images/';
$dh        = opendir($local_dir);

if($dh){
    $conn_id      = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    
    $contents = ftp_nlist($conn_id, "ftp_img/");
    
    foreach($contents as $remote_file){
    $basename = basename($remote_file);
            
        if(!file_exists($local_dir.$basename)){
            $handle = fopen($local_dir.$basename, 'w');
            if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0))
                echo "successfully written to $local_dir$basename\n<br/>";
            else
                echo "There was a problem while downloading $remote_file to $local_file\n<br/>";
            fclose($handle);
        
        }else{
            $remote_filesize = ftp_size($conn_id,$remote_file);
            $local_filesize  = filesize($local_dir.$basename);
            
            if($remote_filesize != $local_filesize){
                $handle = fopen($local_dir.$basename, 'w');
                if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0))
                    echo "successfully written to $local_dir$basename\n<br/>";
                else
                    echo "There was a problem while downloading $remote_file to $local_file\n<br/>";
                fclose($handle);
                
            }else{
                echo 'You have already the <strong>filename</strong>:
                     <font color="blue">'.$basename.'</font> with <strong>Filesize</strong>:
                     <font color="blue">'.$remote_filesize.'</font><br/>';
            }
        }
    }
    ftp_close($conn_id);
}
closedir();
?&gt;

tnx


Messages In This Thread
Copy Files from the Server using FTP in php - by El Forum - 09-17-2008, 01:06 AM
Copy Files from the Server using FTP in php - by El Forum - 09-17-2008, 01:28 AM
Copy Files from the Server using FTP in php - by El Forum - 09-17-2008, 03:32 AM
Copy Files from the Server using FTP in php - by El Forum - 09-17-2008, 03:55 AM
Copy Files from the Server using FTP in php - by El Forum - 09-17-2008, 04:09 AM
Copy Files from the Server using FTP in php - by El Forum - 09-17-2008, 04:23 AM
Copy Files from the Server using FTP in php - by El Forum - 09-17-2008, 04:37 AM
Copy Files from the Server using FTP in php - by El Forum - 09-17-2008, 06:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB