1. 程式人生 > >thinkphp5獲取ftp上的文件列表

thinkphp5獲取ftp上的文件列表

sse user chdir turn 配置 txt resource init iss

引入ftp類

<?php
/**
 * 仿寫CodeIgniter的FTP類
 * FTP基本操作:
 * 1) 登陸;           connect
 * 2) 當前目錄文件列表;  filelist
 * 3) 目錄改變;         chgdir

 *
 * @author quanshuidingdang
 */
class Ftp {

    private $hostname   = ‘‘;
    private $username   = ‘‘;
    private $password   = ‘‘;
    private $port       = 21;
    
private $passive = TRUE; private $debug = TRUE; private $conn_id = FALSE; /** * 構造函數 * * @param array 配置數組 : $config = array(‘hostname‘=>‘‘,‘username‘=>‘‘,‘password‘=>‘‘,‘port‘=>‘‘...); */ public function __construct($config = array()) {
if(count($config) > 0) { $this->_init($config); } } /** * FTP連接 * * @access public * @param array 配置數組 * @return boolean */ public function connect($config = array()) { if(count($config) > 0) { $this->_init($config
); } if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_connect"); } return FALSE; } if( ! $this->_login()) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_login"); } return FALSE; } if($this->passive === TRUE) { ftp_pasv($this->conn_id, TRUE); } return TRUE; } /** * 獲取目錄文件列表 * * @access public * @param string 目錄標識(ftp) * @return array */ public function filelist($path) { if( ! $this->_isconn()) { return FALSE; } return @ftp_nlist($this->conn_id, $path); } //遞歸查詢所有目錄下的文件 public function dir_switch($path = null){ // //路徑 $path = empty($path)?‘.‘:$path; //查看目錄文件 $catalog = $this->filelist($path); // return $catalog; $list = array(); $info = array(); if($catalog){ //遞歸查 foreach ($catalog as $val){ //判斷是否是為目錄 if($this->isFtpDir($val)){ $info = $this->dir_switch($val); if($info){ foreach ($info as $v){ $list[] = $v; } } }else{ $list[] = $val; } // sleep(1); } return $list; } } /** * 目錄改變 * * @access public * @param string 目錄標識(ftp) * @param boolean * @return boolean */ public function chgdir($path = ‘‘, $supress_debug = FALSE) { if ($path == ‘‘ OR !$this->_isconn()) { return FALSE; } $result = @ftp_chdir($this->conn_id, $path); if ($result === FALSE) { if ($this->debug === TRUE AND $supress_debug == FALSE) { $this->_error("ftp_unable_to_chgdir:dir[" . $path . "]"); } return FALSE; } return @ftp_pwd($this->conn_id); // return TRUE; } //查看文件是否存在 function isFtpDir($filename) { if(ftp_size($this->conn_id,$filename)!=-1) { return false; } else { return true; } } /** * 關閉FTP * * @access public * @return boolean */ public function close() { if( ! $this->_isconn()) { return FALSE; } return @ftp_close($this->conn_id); } /** * FTP成員變量初始化 * * @access private * @param array 配置數組 * @return void */ private function _init($config = array()) { foreach($config as $key => $val) { if(isset($this->$key)) { $this->$key = $val; } } //特殊字符過濾 $this->hostname = preg_replace(‘|.+?://|‘,‘‘,$this->hostname); } /** * FTP登陸 * * @access private * @return boolean */ private function _login() { return @ftp_login($this->conn_id, $this->username, $this->password); } /** * 判斷con_id * * @access private * @return boolean */ private function _isconn() { if( ! is_resource($this->conn_id)) { if($this->debug === TRUE) { $this->_error("ftp_no_connection"); } return FALSE; } return TRUE; } /** * 從文件名中獲取後綴擴展 * * @access private * @param string 目錄標識 * @return string */ private function _getext($filename) { if(FALSE === strpos($filename, ‘.‘)) { return ‘txt‘; } $extarr = explode(‘.‘, $filename); return end($extarr); } /** * 從後綴擴展定義FTP傳輸模式 ascii 或 binary * * @access private * @param string 後綴擴展 * @return string */ private function _settype($ext) { $text_type = array ( ‘txt‘, ‘text‘, ‘php‘, ‘phps‘, ‘php4‘, ‘js‘, ‘css‘, ‘htm‘, ‘html‘, ‘phtml‘, ‘shtml‘, ‘log‘, ‘xml‘ ); return (in_array($ext, $text_type)) ? ‘ascii‘ : ‘binary‘; } /** * 錯誤日誌記錄 * * @access prvate * @return boolean */ private function _error($msg) { return @file_put_contents(‘ftp_err.log‘, "date[".date("Y-m-d H:i:s")."]-hostname[".$this->hostname."]-username[".$this->username."]-password[".$this->password."]-msg[".$msg."]\n", FILE_APPEND); } }

放在vendor文件夾下,調用時頁面上:

public function index(){
        set_time_limit(0);
        ini_set(‘memory_limit‘,‘-1‘);
        $config=array(
            ‘hostname‘=>‘127.0.0.1‘,  //ip
            ‘username‘=>‘root‘,
            ‘password‘=>‘root‘,
            ‘port‘=>‘21‘,  //端口號
        );
        vendor(‘ftp‘);
        $ftp=new Ftp();
        $conn=$ftp->connect($config);
        $res=$ftp->filelist(‘file_name‘);  //需要獲取的文件夾名稱
     return $res;
}



thinkphp5獲取ftp上的文件列表