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

[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?
#2

[eluser]xwero[/eluser]
ftp_get
#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
#4

[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.
#5

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

[eluser]magz[/eluser]
Anyway,

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

[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.
#8

[eluser]magz[/eluser]
@xwero,

tnx alot..




Theme © iAndrew 2016 - Forum software by © MyBB