CodeIgniter Forums
Copy Files from the Server using FTP in php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Copy Files from the Server using FTP in php (/showthread.php?tid=11632)



Copy Files from the Server using FTP in php - El Forum - 09-17-2008

[eluser]magz[/eluser]
Hi all.

Need Help, I would like to copy/download files from the
server to local using FTP. But,I dont know how to do it..

Can anyone give some advise or help?


Copy Files from the Server using FTP in php - El Forum - 09-17-2008

[eluser]xwero[/eluser]
ftp_get


Copy Files from the Server using FTP in php - El Forum - 09-17-2008

[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


Copy Files from the Server using FTP in php - El Forum - 09-17-2008

[eluser]xwero[/eluser]
i hope it's not a real username an password.

I see you store it in the local directory images so i assume they are images, then i would use the FTP_BINARY flag.


Copy Files from the Server using FTP in php - El Forum - 09-17-2008

[eluser]magz[/eluser]
waaaahhhh....sorry, i forgot to change it... but i trusted u... and all the codeigniter community...


Copy Files from the Server using FTP in php - El Forum - 09-17-2008

[eluser]magz[/eluser]
Anyway,

What i dont understand is the FTP_BINARY and FTP_ASCII, what are the differnce between them?


Copy Files from the Server using FTP in php - El Forum - 09-17-2008

[eluser]xwero[/eluser]
ASCII content is human readable, BINARY content is not human readable. That would be the non developer description.
So text files are best send in ASCII and images, pdfs but even .doc files should be send BINARY.


Copy Files from the Server using FTP in php - El Forum - 09-17-2008

[eluser]magz[/eluser]
@xwero,

tnx alot..