douxing5199
2017-03-27 11:32
浏览 102
已采纳

在PHP中仅从FTP服务器下载新图像

I made a little script which allows me to download images from FTP server. But, the thing is, whenever I execute the script, ALL images are downloaded. Is there any way to rewrite the code so that it only downloads new images?

My script looks like this:

$ftp_server = "my_server_ip";
$ftp_user = "my_user_name";
$ftp_pass = "my_password";
$DIR="my_path_to_images_folder";

$conn = ftp_connect($ftp_server);
if(!$conn)
{
    exit("Can not connect to server: $ftp_server
");
}

if(!ftp_login($conn,$ftp_user,$ftp_pass))
{
    ftp_quit($conn);
    exit("Can't login
");
}

ftp_chdir($conn,$DIR);

$files = ftp_nlist($conn,'.');
for($i=0;$i<count($files);$i++)
{
    if(!ftp_get($conn,$files[$i],$files[$i],FTP_BINARY))
    {
        echo "Can't download {$files[$i]}
";
    }
    else
    {
        echo "Successfully transferred images!";
    }
}

ftp_quit($conn);

Thank you.

图片转代码服务由CSDN问答提供 功能建议

我制作了一个小脚本,允许我从FTP服务器下载图像。 但是,问题是,每当我执行脚本时,都会下载所有图像。 有没有办法重写代码,以便它只下载新的图像?

我的脚本如下所示:

  $ ftp_server =“my_server_ip”; 
 $ ftp_user =“my_user_name”; 
 $  ftp_pass =“my_password”; 
 $ DIR =“my_path_to_images_folder”; 
 
 $ conn = ftp_connect($ ftp_server); 
if(!$ conn)
 {
 exit(“无法连接到服务器:$  ftp_server 
“); 
} 
 
if(!ftp_login($ conn,$ ftp_user,$ ftp_pass))
 {
 ftp_quit($ conn); 
 exit(”无法登录
“  ); 
} 
 
 
ftp_chdir($ conn,$ DIR); 
 
 $ files = ftp_nlist($ conn,'。'); 
for($ i = 0; $ i&lt; count($ files)  ; $ i ++)
 {
 if if(!ftp_get($ conn,$ files [$ i],$ files [$ i],FTP_BINARY))
 {
 echo“无法下载{$ files [$  i]} 
“; 
} 
 
 
 
 
 
 
 
 
 
 
 
 \ {
 
 
 
 
 
 
 
 
 
 
 
 
呼叫”成功传输图像!  > 
 
 

谢谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • dsbj66959 2017-03-27 11:56
    最佳回答

    To download only files that do not exist locally yet, or are newer than the local copy, use:

    $files = ftp_nlist($conn, '.');
    
    foreach ($files as $file)
    {
        $remote_time = ftp_mdtm($conn, $file);
    
        if (!file_exists($file) ||
            (filemtime($file) < $remote_time))
        {
            ftp_get($conn, $file, $file, FTP_BINARY);
            touch($file, $remote_time);
        }
    }
    

    If your server supports MLSD command and you have PHP 7.2 and newer, you can replace ftp_nlist and repeated call to ftp_mdtm with one efficient call to ftp_mlsd function.

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题