Home  >  Article  >  Backend Development  >  Simple example of using ftp to download files in php

Simple example of using ftp to download files in php

WBOY
WBOYOriginal
2016-07-25 09:00:231295browse
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;
}
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn