Home > Article > Backend Development > Simple example of using ftp to download files in php
Let me give you an example of using PHP ftp function to download files, and practice the usage of FTP function in PHP for the reference of beginners.
The code is as follows: <?php /** * ftp下载文件 * site bbs.it-home.org */ function downfromftp($file){ //连接服务器 $conn_id = ftp_connect(“123.xxx.xxx.99”); //远程FTP的IP地址 //登陆 $login_result = ftp_login($conn_id, “1″, “kkk”); //更改下载目录 if($login_result){ $res=ftp_chdir($conn_id, “anony_ftp”); if(!$res){ exit(‘更改目录失败’); } } // 下载文件 $local_file = ‘down/’.$file; $server_file = $file; if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) { ftp_close($conn_id); fclose($fp); return true; } else { ftp_close($conn_id); fclose($fp); return false; } } ?> |