FTP は、標準 (アクティブ、アクティブ モード) とパッシブ (PASV、パッシブ モード) と呼ばれる 2 つのモードをサポートするファイル転送プロトコルです。標準モードの FTP クライアントは、FTP サーバーに PORT コマンドを送信します。パッシブ モードの FTP クライアントは PASV コマンドを FTP サーバーに送信します。
以下は、これら 2 つの方法の動作原理の紹介です:
コードをコピーします コードは次のとおりです:
// FTP サーバーに接続します
$conn = ftp_connect(ftp.server.com);
// ユーザー名とパスワードを使用してログインします
ftp_login($conn, "john", "doe");
// リモート システムのタイプを取得します
ftp_systype($conn);
// ファイルをリストします
$filelist = ftp_nlist($conn, “.”); // ファイルをダウンロードします
ftp_get($conn, “data .zip", "data.zip", FTP_BINARY);
// 接続を閉じる
ftp_quit($conn);
// FTP 接続を初期化します。PHP には、ホスト名とパラメータとしてポートを指定します。上記の例では、ホスト名は「ftp.server.com」ですが、ポートが指定されていない場合、PHP は接続を確立するためにデフォルトのポートとして「21」を使用します。
//接続が成功すると、ftp_connect() はハンドル ハンドルを返します。このハンドルは、後で使用される FTP 関数によって使用されます。
$conn = ftp_connect(ftp.server.com);
// 接続が確立されたら、ftp_login() を使用してユーザー名とユーザー パスワードを送信します。関数 ftp_login() は、ftp_connect() 関数で渡されたハンドルを使用して、ユーザー名とパスワードを正しいサーバーに送信できるかどうかを判断していることがわかります。
ftp_login($conn, “john”, “doe”);
// 接続を閉じる
ftp_quit($conn);
// FTP サーバーにログインした後、PHP は、次の情報を取得できるいくつかの関数を提供します。システムとファイルとディレクトリの情報。
ftp_pwd()
//現在のディレクトリを取得します
$here = ftp_pwd($conn);
//サーバー側のシステム情報を取得します ftp_systype()
$server_os = ftp_systype($conn);モード (PASV) スイッチ、PASV をオンまたはオフにします (1 はオンを意味します)
ftp_pasv($conn, 1);
// ftp_chdir() 関数を使用してディレクトリに入り、パラメータとしてディレクトリ名を受け取ります。
ftp_chdir($conn, “public_html”);
// ftp_cdup() を使用して現在のディレクトリの親ディレクトリに戻ります
ftp_cdup($conn)
// ディレクトリを作成または移動するには、ftp_mkdir() を使用しますftp_rmdir() 関数; 注: ftp_mkdir() が正常に作成されると、新しく作成されたディレクトリ名が返されます。
ftp_mkdir($conn, “test”);
ftp_rmdir($conn, “test”);
// ftp_put() 関数は、アップロードするローカル ファイル名を指定する必要があります。 . 次のファイル名と転送の種類。例: ファイル「abc.txt」をアップロードし、アップロード後に「xyz.txt」という名前を付ける場合、コマンドは次のようになります:
ftp_put($conn, "xyz.txt", "abc.txt" , FTP_ASCII) ;
//ファイルのダウンロード: PHP によって提供される関数は ftp_get() ですが、これにはサーバー上のファイル名、ダウンロード後のファイル名、および転送タイプもパラメーターとして必要です。サイド ファイルは his.zip です。ローカル マシンにダウンロードして hers.zip という名前を付ける場合、コマンドは次のとおりです:
ftp_get($conn, “hers.zip”, “his.zip”, FTP_BINARY) ;
//PHP には 2 つの方法があります。1 つは単にファイル名とディレクトリをリストする方法、もう 1 つはファイル サイズ、権限、作成時間、その他の情報を詳細にリストする方法です。
//最初の関数は ftp_nlist() 関数を使用し、2 番目の関数は ftp_rawlist() を使用します。どちらの関数もパラメーターとしてディレクトリ名を必要とし、どちらも配列の各要素を返します。行のリスト。
$filelist = ftp_nlist($conn, “.”);
//BITES を単位として指定したファイルのサイズを返す関数 ftp_size()。 "-1" が返された場合、これはディレクトリ
$filelist = ftp_size($conn, "data.zip")
?>
FTP クラス
であることを意味することに注意してください。コードをコピーします
コードは次のとおりです:
/**
* CodeIgniter の FTP クラスを模倣します
* 基本的な FTP 操作:
* 1) ログイン; 接続
* 2) 現在のディレクトリファイルリスト; filelist
* 4) 名前変更/移動
* 5 ) フォルダーを作成します
* 6) delete_dir/delete_file
* 7) アップロードします。*/
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->ホスト名, $this->port))) {
if($this->debug === TRUE) {
$this->gt;_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)
* @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 TRUE;
}
/**
* ディレクトリ生成
*
* @access public
* @param string ディレクトリ識別(ftp)
* @param int ファイル権限リスト
* @return boolean
*/
public function mkdir($path = '', $permissions = NULL) {
if($path == '' OR ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_mkdir($this->conn_id, $path);
if($result === FALSE) {
if( $this->debug === TRUE) {
$this->_error("ftp_unable_to_mkdir:dir[".$path."]");
}
return FALSE;
}
if( ! is_null( $permissions)) {
$this->chmod($path,(int)$permissions);
}
return TRUE;
}
/**
* Upload
*
* @access public
* @param string ローカルディレクトリ識別子
* @param string リモートディレクトリ識別子 (ftp)
* @param string アップロードモード auto || アップロード後のファイル権限 リスト
* @return boolean
*/
public function Upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
if( ! $this->_isconn()) {
return FALSE;
}
if( ! file_exists($localpath)) {
if ($this->debug === TRUE) {
$this->_error("ftp_no_source_file:".$localpath);
}
return FALSE;
}
if($mode == 'auto') {
$ext = $this->gt;_getext($localpath);
$mode = $this->gt;_settype($ext);
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
}
return FALSE;
}
if ( ! is_null($permissions)) {
$this->chmod($remotepath,(int)$permissions);
}
return TRUE;
}
/**
* ダウンロード
*
* @access public
* @param 文字列リモートディレクトリ識別子 (ftp)
* @param 文字列ローカルディレクトリ識別子
* @param 文字列ダウンロードモード自動 || boolean
*/
public function download ($remotepath, $localpath, $mode = 'auto') {
if( ! $this->_isconn()) {
return FALSE;
}
if($mode == 'auto') {
$ ext = $this->_getext($remotepath);
$mode = $this->_settype($ext);
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
}
return FALSE;
}
return TRUE;
}
/**
* 名前変更/移動
*
* @access public
* @param string リモートディレクトリ識別 (ftp)
* @param string 新しいディレクトリ識別
* @param boolean 名前を変更する (FALSE) か移動する (TRUE) かを決定します
* @return boolean
*/
public function rename($oldname, $newname, $move = FALSE) {
if( ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_rename($this->conn_id, $oldname, $newname);
if($result === FALSE) {
if($this->debug === TRUE) {
$msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
$this->_error($msg);
}
return FALSE;
}
return TRUE;
}
/**
* ファイルを削除
*
* @access public
* @param 文字列ファイル識別子 (ftp)
* @return boolean
*/
public function delete_ファイル( $file) {
if( ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_delete($this->conn_id, $file);
if($result = == FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_delete_file:file[".$file."]");
}
return FALSE;
}
return TRUE;
}
/**
* フォルダーを削除
*
* @access public
* @param string ディレクトリ ID (ftp)
* @return boolean
*/
public function delete_dir($path) {
if( ! $this->_isconn()) {
return FALSE;
}
/ /对目录宏的'/'文字追加反斜杠''
$path = preg_replace("/(.+?)/*$/", "\1/", $path);
//获取目录文例列表
$filelist = $this->filelist($path);
if($filelist !== FALSE AND count($filelist) > 0) {
foreach($filelist as $item) {
/ /如果我们無法删除,那么就可能是一文件夹
//所以我们递归调用delete_dir()
if( ! @delete_file($item)) {
$this->delete_dir($item);
}
}
}
//删除文件夹(空文件夹)
$result = @ftp_rmdir($this->conn_id, $path);
if($result === FALSE) {
if($this->gt; debug === TRUE) {
$this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
}
return FALSE;
}
return TRUE;
}
/**
* ファイル権限を変更します
*
* @access public
* @param string ディレクトリ識別子 (ftp)
* @return boolean
*/
public function chmod($path, $perm) {
if( ! $this->_isconn()) {
return FALSE;
}
//只有PHP5中才定义了修改权制限的関数数(ftp)
if( ! function_exists('ftp_chmod')) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_chmod(function)");
}
return FALSE;
}
$result = @ftp_chmod($this->conn_id, $perm, $path);
if($result === FALSE) {
if($this->debug = == TRUE) {
$this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");
}
return FALSE;
}
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);
}
/**
* FTPを閉じる
*
* @access public
* @return boolean
*/
public function close() {
if( ! $this->_isconn()) {
return FALSE;
}
return @ftp_close($this->conn_id);
}
/**
* FTPメンバー変数の初期化
*
* @access private
* @param配列設定配列
* @return void
*/
private function _init($config = array()) {
foreach($config as $key => $val) {
if(isset($this->$key)) {
$this->gt;$key = $val;
}
}
//特殊文字过滤
$this->hostname = preg_replace ('|.+?://|','',$this->ホスト名);
}
/**
* FTP ログイン
*
* @access private
* @return boolean
*/
private function _login() {
return @ftp_login($this- >conn_id, $this->ユーザー名, $this->パスワード);
}
/**
* con_id を決定します
*
* @access private
* @return boolean
*/
プライベート関数 _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
*/
プライベート関数 _getext($filename) {
if(FALSE === strpos($filename, '.')) {
return 'txt';
}
$extarr =explode('.' , $filename);
return end($extarr);
}
/**
* サフィックス拡張子 ascii またはバイナリから FTP 転送モードを定義します
*
* @access private
* @param 文字列サフィックス拡張子
* @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")."]-ホスト名[".$this->ホスト名."]-ユーザー名[".$this->ユーザー名."]-パスワード[".$this->パスワード." ]-msg[".$msg."]n", FILE_APPEND);
}
}
/*ファイルの終わり ftp.php*/
/*Location /Apache Group/htdocs/ftp.php*/
デモ
复制代価代価如下:
require_once('ftp.php');
$config = array(
'hostname' => 'localhost',
'username' => 'root',
'パスワード' => 'root',
'ポート' =>
$ftp = new Ftp();
$ftp->connect($config);
$ftp->upload('ftp_err.log','ftp_upload.log');
$ftp->download('ftp_upload.log','ftp_download.log');
/*ファイル ftp_demo の終わり。 php*/
/*場所: /htdocs/ftp_demo.php*/
http://www.bkjia.com/PHPjc/327631.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327631.html技術記事 FTP はファイル転送プロトコルの 1 つで、標準モード (つまりアクティブ、主動作方式) とパッシブ (PASV、被駆動方式) の 2 つのモードをサポートしています。