欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php使用ftp遠(yuǎn)程上傳文件類(完美解決主從文件同步問(wèn)題的方法)

 更新時(shí)間:2016年09月23日 07:07:21   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇php使用ftp遠(yuǎn)程上傳文件類(完美解決主從文件同步問(wèn)題的方法)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

php使用ftp實(shí)現(xiàn)文件上傳代碼片段:

<?php 
  /**
   * ftp上傳文件類
   */ 
  class Ftp { 

    /**
     * 測(cè)試服務(wù)器
     * 
     * @var array
     */ 
    private $testServer = array( 
      'host' => 'ip', 
      'port' => 21, 
      'user' => 'userName', 
      'pwd' => 'password' 
    ); 

    /**
     * 打開(kāi)并登錄服務(wù)器
     * 
     * @param string $flag 服務(wù)器標(biāo)識(shí)test
     * @return mixed 
     *       0:服務(wù)器連接失敗
     *       1:服務(wù)器登錄失敗
     *       resource 連接標(biāo)識(shí)
     */ 
    public function openServer($flag = 'test'){ 
      //選擇服務(wù)器 
      $config = $this->getServerConfig($flag); 

      //連接服務(wù)器 
      $connect = ftp_connect($config['host'], $config['port']); 
      if($connect == false) return 0; 

      //登錄服務(wù)器 
      if(!ftp_login($connect, $config['user'], $config['pwd'])) return 1; 

      //打開(kāi)被動(dòng)模式,數(shù)據(jù)的傳送由客戶機(jī)啟動(dòng),而不是由服務(wù)器開(kāi)始 
      ftp_pasv($connect, true); 

      //返回連接標(biāo)識(shí) 
      return $connect; 
    } 

    /**
     * 創(chuàng)建目錄并將目錄定位到當(dāng)請(qǐng)目錄
     * 
     * @param resource $connect 連接標(biāo)識(shí)
     * @param string $dirPath 目錄路徑
     * @return mixed 
     *       2:創(chuàng)建目錄失敗
     *       true:創(chuàng)建目錄成功
     */ 
    public function makeDir($connect, $dirPath){ 
      //處理目錄 
      $dirPath = '/' . trim($dirPath, '/'); 
      $dirPath = explode('/', $dirPath); 
      foreach ($dirPath as $dir){ 
        if($dir == '') $dir = '/'; 
        //判斷目錄是否存在 
        if(@ftp_chdir($connect, $dir) == false){ 
          //判斷目錄是否創(chuàng)建成功 
          if(@ftp_mkDir($connect, $dir) == false){ 
            return 2; 
          } 
          @ftp_chdir($connect, $dir); 
        } 
      } 
      return true; 
    } 

    /**
     * 關(guān)閉服務(wù)器
     * 
     * @param resource $connect 連接標(biāo)識(shí)
     */ 
    public function closeServer($connect){ 
      if(!empty($connect)) ftp_close($connect); 
    } 

    /**
     * 上傳文件
     * 
     * @param string $flag 服務(wù)器標(biāo)識(shí)
     * @param string $local 上傳文件的本地路徑
     * @param string $remote 上傳文件的遠(yuǎn)程路徑
     * @return int 
     *       0:服務(wù)器連接失敗 
     *       1:服務(wù)器登錄失敗
     *       2:創(chuàng)建目錄失敗
     *       3:上傳文件失敗
     *       4:上傳成功
     */ 
    public function upload($flag = 'test', $local, $remote){ 
      //連接并登錄服務(wù)器 
      $connect = $this->openServer($flag); 
      if(($connect === 0) || ($connect === 1)) return $connect; 

      //上傳文件目錄處理 
      $mdr = $this->makeDir($connect, dirname($remote)); 
      if($mdr === 2) return 2; 

      //上傳文件 
      $result = ftp_put($connect, basename($remote), $local, FTP_BINARY); 

      //關(guān)閉服務(wù)器 
      $this->closeServer($connect); 

      //返回結(jié)果 
      return (!$result) ? 3 : 4; 
    } 

    /**
     * 刪除文件
     * 
     * @param string $flag 服務(wù)器標(biāo)識(shí)
     * @param string $remote 文件的遠(yuǎn)程路徑
     * @return int 
     *       0:服務(wù)器連接失敗 
     *       1:服務(wù)器登錄失敗
     *       2:刪除失敗
     *       3:刪除成功
     */ 
    public function delete($flag = 'test', $remote){ 
      //連接并登錄服務(wù)器 
      $connect = $this->openServer($flag); 
      if(($connect === 0) || ($connect === 1)) return $connect; 

      //刪除 
      $result = ftp_delete($connect, $remote); 

      //關(guān)閉服務(wù)器 
      $this->closeServer($connect); 

      //返回結(jié)果 
      return (!$result) ? 2 : 3; 
    } 

    /**
     * 讀取文件
     * 
     * @param string $flag 服務(wù)器標(biāo)識(shí)
     * @param string $remote 文件的遠(yuǎn)程路徑
     * @return mixed 
     *       0:服務(wù)器連接失敗 
     *       1:服務(wù)器登錄失敗
     */ 
    public function read($flag, $remote){ 
      //連接并登錄服務(wù)器 
      $connect = $this->openServer($flag); 
      if(($connect === 0) || ($connect === 1)) return $connect; 

      //讀取 
      $result = ftp_nlist($connect, $remote); 

      //關(guān)閉服務(wù)器 
      $this->closeServer($connect); 

      //返回結(jié)果 
      foreach ($result as $key => $value){ 
        if(in_array($value, array('.', '..'))) unset($result[$key]); 
      } 
      return array_values($result); 
    } 

    /**
     * 獲取ftp服務(wù)器配置
     * 
     * @param string $flag 服務(wù)器標(biāo)識(shí)test
     * @return array ftp服務(wù)器連接配置
     */ 
    private function getServerConfig($flag = 'test'){ 
      $flag = strtolower($flag); 
      //測(cè)試服務(wù)器 
      if($flag == 'test') return $this->testServer; 
      //默認(rèn)返回測(cè)試服務(wù)器 
      return $this->testServer; 
    } 
  }
  ?>

以上就是小編為大家?guī)?lái)的php使用ftp遠(yuǎn)程上傳文件類(完美解決主從文件同步問(wèn)題的方法)的全部?jī)?nèi)容了,希望對(duì)大家有所幫助,多多支持腳本之家~

相關(guān)文章

最新評(píng)論