av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語言

PHP中FTP操作類代碼

時間:2025-03-03 16:05:03 php語言 我要投稿
  • 相關(guān)推薦

PHP中FTP操作類代碼

  文章主要介紹了php中實現(xiàn)FTP功能,包括上傳,拷貝,移動,刪除文件與創(chuàng)建目錄功能,需要的朋友可以參考下.就跟隨百分網(wǎng)小編一起去了解下吧,想了解更多相關(guān)信息請持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  復(fù)制代碼 代碼如下:

  <?php

  /**

  * 作用:FTP操作類( 拷貝、移動、刪除文件/創(chuàng)建目錄 )

  * QQ:276624915

  */

  class class_ftp

  {

  public $off; // 返回操作狀態(tài)(成功/失敗)

  public $conn_id; // FTP連接

  /**

  * 方法:FTP連接

  * @FTP_HOST -- FTP主機

  * @FTP_PORT -- 端口

  * @FTP_USER -- 用戶名

  * @FTP_PASS -- 密碼

  */

  function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS)

  {

  $this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("FTP服務(wù)器連接失敗");

  @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP服務(wù)器登陸失敗");

  @ftp_pasv($this->conn_id,1); // 打開被動模擬

  }

  /**

  * 方法:上傳文件

  * @path -- 本地路徑

  * @newpath -- 上傳路徑

  * @type -- 若目標(biāo)目錄不存在則新建

  */

  function up_file($path,$newpath,$type=true)

  {

  if($type) $this->dir_mkdirs($newpath);

  $this->off = @ftp_put($this->conn_id,$newpath,$path,FTP_BINARY);

  if(!$this->off) echo "文件上傳失敗,請檢查權(quán)限及路徑是否正確!";

  }

  /**

  * 方法:移動文件

  * @path -- 原路徑

  * @newpath -- 新路徑

  * @type -- 若目標(biāo)目錄不存在則新建

  */

  function move_file($path,$newpath,$type=true)

  {

  if($type) $this->dir_mkdirs($newpath);

  $this->off = @ftp_rename($this->conn_id,$path,$newpath);

  if(!$this->off) echo "文件移動失敗,請檢查權(quán)限及原路徑是否正確!";

  }

  /**

  * 方法:復(fù)制文件

  * 說明:由于FTP無復(fù)制命令,本方法變通操作為:下載后再上傳到新的路徑

  * @path -- 原路徑

  * @newpath -- 新路徑

  * @type -- 若目標(biāo)目錄不存在則新建

  */

  function copy_file($path,$newpath,$type=true)

  {

  $downpath = "c:/tmp.dat";

  $this->off = @ftp_get($this->conn_id,$downpath,$path,FTP_BINARY);// 下載

  if(!$this->off) echo "文件復(fù)制失敗,請檢查權(quán)限及原路徑是否正確!";

  $this->up_file($downpath,$newpath,$type);

  }

  /**

  * 方法:刪除文件

  * @path -- 路徑

  */

  function del_file($path)

  {

  $this->off = @ftp_delete($this->conn_id,$path);

  if(!$this->off) echo "文件刪除失敗,請檢查權(quán)限及路徑是否正確!";

  }

  /**

  * 方法:生成目錄

  * @path -- 路徑

  */

  function dir_mkdirs($path)

  {

  $path_arr = explode('/',$path); // 取目錄數(shù)組

  $file_name = array_pop($path_arr); // 彈出文件名

  $path_p = count($path_arr); // 取層數(shù)

  foreach($path_arr as $val) // 創(chuàng)建目錄

  {

  if(@ftp_chdir($this->conn_id,$val) == FALSE)

  {

  $tmp = @ftp_mkdir($this->conn_id,$val);

  if($tmp == FALSE)

  {

  echo "目錄創(chuàng)建失敗,請檢查權(quán)限及路徑是否正確!";

  exit;

  }

  @ftp_chdir($this->conn_id,$val);

  }

  }

  for($i=1;$i=$path_p;$i++) // 回退到根

  {

  @ftp_cdup($this->conn_id);

  }

  }

  /**

  * 方法:關(guān)閉FTP連接

  */

  function close()

  {

  @ftp_close($this->conn_id);

  }

  }// class class_ftp end

  /************************************** 測試 ***********************************

  $ftp = new class_ftp('192.168.100.143',21,'user','pwd'); // 打開FTP連接

  //$ftp->up_file('aa.txt','a/b/c/cc.txt'); // 上傳文件

  //$ftp->move_file('a/b/c/cc.txt','a/cc.txt'); // 移動文件

  //$ftp->copy_file('a/cc.txt','a/b/dd.txt'); // 復(fù)制文件

  //$ftp->del_file('a/b/dd.txt'); // 刪除文件

  $ftp->close(); // 關(guān)閉FTP連接

  *****************************************************************************/

  ?>

【PHP中FTP操作類代碼】相關(guān)文章:

php分頁類代碼09-08

php中ftp-chdir() ftp-cdup()函數(shù)用法07-02

PHP經(jīng)典常用特效類代碼07-27

PHP中的Reload操作06-26

PHP 中的加密技術(shù)及代碼11-07

PHP中如何實現(xiàn)crontab代碼05-30

php中使用redis隊列操作實例代碼05-16

PHP中如何定義類及其成員屬性與操作09-23

如何在HTML中嵌入PHP代碼11-07