首頁 php教程 PHP源码 最新最全的PHP FTP操作类,来自EaglePHP框架

最新最全的PHP FTP操作类,来自EaglePHP框架

May 25, 2016 pm 05:04 PM

最新最全的PHP FTP操作类,来自EaglePHP框架

<?php

/**
 * 
 * EaglePHP FTP操作类
 * 
 * @author maojianlw@139.com
 * @link http://www.eaglephp.com
 * @since 2013-06-18
 */

class Ftp
{
	
	/**
	 * 
	 * 用户名
	 * 
	 * @var string
	 */
	private static $username = null;
	
	
	/**
	 * 
	 * 密码
	 * 
	 * @var string
	 */
	private static $password = null;
	
	
	/**
	 * 
	 * 连接的服务器
	 * 
	 * @var string
	 */
	private static $host = null;
	
	/**
	 * 
	 * 连接资源句柄
	 * 
	 * @var resource
	 */
	private static $resource = null;
	
	
	/**
	 * 
	 * 连接并登陆ftp服务器
	 * 
	 * @param string $host
	 * @param string $username
	 * @param string $password
	 * @param int $port
	 * @param int $timeout
	 * @param bool $pasv
	 * @return bool
	 */
	public static function connect($host, $username, $password, $port=21, $timeout=90, $pasv = false)
	{
		if(!extension_loaded(&#39;ftp&#39;)) throw_exception(language(&#39;SYSTEM:module.not.loaded&#39;, array(&#39;ftp&#39;)));
        if((self::$resource = ftp_connect($host, $port, $timeout)) === false) throw_exception(&#39;ftp_unable_to_connect&#39;);
        if(!self::login($username, $password)) throw_exception(&#39;ftp_unable_to_login&#39;);
        if($pasv === true) self::pasv($pasv);
        return true;
	}
	
	
	/**
	 * 
	 * 登录 FTP 服务器
	 * 
	 * @param string $username
	 * @param string $password
	 * @return bool
	 */
	public static function login($username, $password)
	{
		return ftp_login(self::$resource, $username, $password);
	}
	
	
	/**
	 * 
	 * 切换目录至当前目录的父目录 (上级目录)
	 * 
	 * @return bool
	 */
	public static function cdup()
	{
		return ftp_cdup(self::$resource);
	}
	
	
	/**
	 * 
	 * 将当前目录切换为指定的目录
	 * 
	 * @param string $directory
	 * @return bool
	 */
	public static function chdir($directory)
	{
		return ftp_chdir(self::$resource, $directory);
	}
	
	/**
	 * 
	 * 设置在指定的远程文件的权限模式
	 * 
	 * @param int $mode
	 * @param string $filename
	 * @return bool
	 */
	public static function chmod($mode, $filename)
	{
		return ftp_chmod(self::$resource, $mode, $filename);
	}
	
	
	/**
	 * 
	 * 关闭ftp连接标识符并释放资源
	 * 
	 * @return bool
	 */
	public static function close()
	{
		return ftp_close(self::$resource);
	}
	
	
	/**
	 * 
	 *  ftp_close() 的 别名
	 *  
	 *  @return bool
	 */
	public static function quit()
	{
		return self::close();
	}
	
	
	/**
	 * 
	 * 删除 FTP 服务器上的一个文件
	 * 
	 * @param string $path
	 * @return bool
	 */
	public static function delete($path)
	{
		return ftp_delete(self::$resource, $path);
	}
	
	
	/**
	 * 
	 * 请求运行一条 FTP 命令
	 * 
	 * @param string $command
	 * @return bool
	 */
	public static function exec($command)
	{
		return ftp_exec(self::$resource, $command);
	}
	
	/**
	 * 
	 * 发送到FTP服务器的任意命令
	 * 
	 * @param string $command
	 * @return array
	 */
	public static function raw($command)
	{
		return ftp_raw(self::$resource, $command);
	}
	
	
	/**
	 * 
	 * 从 FTP 服务器上下载一个文件并保存到本地一个已经打开的文件中
	 * 
	 * @param resource $handle 本地已经打开的文件的句柄
	 * @param string $remote_file  远程文件
	 * @param int $mode  传送模式参数 mode 必须是 (文本模式) FTP_ASCII 或 (二进制模式) FTP_BINARY 中的一个
	 * @param int $resumepos
	 * @return bool
	 */
	public static function fget($handle, $remote_file, $mode=FTP_ASCII, $resumepos=0)
	{
		return ftp_fget(self::$resource, $handle, $remote_file, $mode, $resumepos);
	}
	
	
	/**
	 * 
	 * 上传一个已经打开的文件到 FTP 服务器
	 * 
	 * @param string $remote_file
	 * @param resource $handle
	 * @param int $mod
	 * @param int $startpos
	 * @return bool
	 */
	public static function fput($remote_file, $handle, $mod=FTP_ASCII, $startpos=0)
	{
		return ftp_fput(self::$resource, $remote_file, $handle, $mode, $startpos);
	}
	
	
	/**
	 * 
	 * 返回当前 FTP 连接的各种不同的选项设置
	 * 
	 * @param int $option
	 * @return mixed
	 */
	public static function getOption($option)
	{
		return ftp_get_option(self::$resource, $option);
	}
	
	
	/**
	 * 
	 * 设置各种 FTP 运行时选项
	 * 
	 * @param int $option
	 * @param mixed $value
	 * @return bool
	 */
	public static function setOption($option, $value)
	{
		return ftp_set_option(self::$resource, $option, $value);
	}
	
	
	/**
	 * 
	 * 从 FTP 服务器上下载一个文件
	 * 
	 * @param string $local_file
	 * @param string $remote_file
	 * @param int $mode
	 * @param int $resumepos
	 * @return bool
	 */
	public static function get($local_file, $remote_file, $mode=FTP_ASCII, $resumepos=0)
	{
		return ftp_get(self::$resource, $local_file, $remote_file, $mode, $resumepos);	
	}
	
	
	
	/**
	 * 
	 * 上传文件到 FTP 服务器
	 * 
	 * @param string $remote_file
	 * @param string $local_file
	 * @param int $mode
	 * @param int $startpos
	 * @return bool
	 */
	public static function put($remote_file, $local_file, $mode=FTP_ASCII, $startpos=0)
	{
		return ftp_put(self::$resource, $remote_file, $local_file, $mode, $startpos);
	}
	
	
	/**
	 * 
	 * 返回指定文件的最后修改时间
	 * 
	 * @param string $remote_file
	 */
	public static function mdtm($remote_file)
	{
		return ftp_mdtm(self::$resource, $remote_file);
	}
	
	
	
	/**
	 * 
	 * 递归创建目录并授权
	 * 
	 * @param string $directory
	 * @param int $mode 权限模式
	 * @return string 如果成功返回新建的目录名,否则返回 FALSE
	 */
	public static function mkdir($directory, $mode=null)
	{
		if(empty($directory)) return false;
	    if(self::isDir($directory)) return true;
	    if(!self::mkdir(dirname($directory), $mode)) return false;
	    if(!ftp_mkdir(self::$resource, $directory)) return false;
	    if(!is_null($mode)) self::chmod($mode, $directory);
		return true;
	}
	
	
	/**
	 * 
	 *  递归删除 FTP服务器上的目录
	 *  
	 * @param string $directory
	 * @return bool
	 */
	public static function rmdir($directory)
	{
		$list = self::nlist($directory);
		if($list !== false && count($list) > 0)
		{
			foreach ($list as $k=>$v)
			{
				$path = "{$directory}/{$v}";
				if(self::isDir($path)) self::rmdir($path);
				else self::delete($path);
			}
		}
		return ftp_rmdir(self::$resource, $directory);
	}
	
	
	/**
	 * 
	 * 连续获取/发送文件(non-blocking)
	 * 以不分块的方式连续获取/发送一个文件。 
	 * 
	 * @return int 返回常量 FTP_FAILED 或 FTP_FINISHED 或 FTP_MOREDATA
	 */
	public static function nbContinue()
	{
		return ftp_nb_continue(self::$resource);
	}
	
	
	/**
	 * 从检索FTP服务器上的文件并将其写入一个打开的文件(非阻塞)
	 * 
	 * @param resource $handle
	 * @param string $remote_file
	 * @param int $mode
	 * @param int $resumepos
	 * @return int 返回ftp_failed或ftp_finished或ftp_moredata
	 */
	public static function nbFget($handle, $remote_file, $mode=FTP_ASCII, $resumepos=0)
	{
		return ftp_nb_fget(self::$resource, $handle, $remote_file, $mode, $resumepos);
	}
	
	
	/**
	 * 
	 * 从打开的文件到FTP服务器上的文件(非阻塞)
	 * 
	 * @param unknown_type $remote_file
	 * @param unknown_type $handle
	 * @param unknown_type $mode
	 * @param unknown_type $startpos
	 */
	public static function nbFput($remote_file, $handle, $mode=FTP_ASCII, $startpos=0)
	{
		return ftp_nb_fput(self::$resource, $remote_file, $handle, $mode, $startpos);
	}
	
	
	
	/**
	 * 
	 * 从 FTP 服务器上获取文件并写入本地文件(non-blocking)
	 * 
	 * @param string $local_file
	 * @param string $remote_file
	 * @param int $mode
	 * @param int $resumepos
	 * @return int  返回 FTP_FAILED,FTP_FINISHED 或 FTP_MOREDATA
	 */
	public static function nbGet($local_file, $remote_file, $mode=FTP_ASCII, $resumepos=0)
	{
		return ftp_nb_get(self::$resource, $local_file, $remote_file, $mode, $resumepos);
	}
	
	
	/**
	 * 
	 * 存储一个文件至 FTP 服务器(non-blocking)
	 * 
	 * @param string $remote_file
	 * @param string $local_file
	 * @param int $mode
	 * @param int $startpos
	 * @return int  返回 FTP_FAILED,FTP_FINISHED 或 FTP_MOREDATA
	 */
	public static function nbPut($remote_file, $local_file, $mode=FTP_ASCII, $startpos=0)
	{
		return ftp_nb_put(self::$resource, $remote_file, $local_file, $mode, $startpos);
	}
	
	
	/**
	 *
	 * 返回给定目录的文件列表
	 * 
	 * @param string $directory
	 * @return array
	 */
	public static function nlist($directory)
	{
		return ftp_nlist(self::$resource, $directory);
	}
	
	
	/**
	 * 
	 * 返回当前 FTP 被动模式是否打开
	 * 在被动模式打开的情况下,数据的传送由客户机启动,而不是由服务器开始。 
	 * 
	 * @param bool $pasv
	 * @return bool
	 */
	public static function pasv($pasv)
	{
		return ftp_pasv(self::$resource, $pasv);
	}
	
	
	/**
	 * 
	 * 返回当前目录名
	 * 
	 * @return string
	 */
	public static function pwd()
	{
		return ftp_pwd(self::$resource);
	}
	
	
	/**
	 * 
	 * 返回指定目录下文件的详细列表
	 * 将执行 FTP LIST 命令,并把结果做为一个数组返回
	 * 
	 * @param string $directory
	 * @return array
	 */
	public static function rawlist($directory)
	{
		return ftp_rawlist(self::$resource, $directory);
	}
	
	
	/**
	 * 
	 * 更改 FTP 服务器上的文件或目录名
	 * 
	 * @param string $oldname
	 * @param string $newname
	 * @return bool
	 */
	public static function rename($oldname, $newname)
	{
		return ftp_rename(self::$resource, $oldname, $newname);
	}
	
	
	/**
	 * 
	 * 向服务器发送 SITE 命令
	 * 
	 * @param string $command
	 * @return bool
	 */
	public static function site($command)
	{
		return ftp_site(self::$resource, $command);
	}
	
	
	/**
	 * 
	 * 返回指定文件的大小
	 * 
	 * @param string $remote_file
	 * @return int  如果指定文件不存在或发生错误,则返回 -1。有些 FTP 服务器可能不支持此特性。 
	 */
	public static function size($remote_file)
	{
		return ftp_size(self::$resource, $remote_file);
	}
	
	
	/**
	 * 
	 * 打开一个安全的ssl-ftp连接
	 * 注:此函数需要OpenSSL扩展库支持
	 * 
	 * @param string $host
	 * @param int $port
	 * @param int $timeout
	 * @return resource
	 */
	public static function sslConnect($host, $port=21, $timeout=90)
	{
		return ftp_ssl_connect($host, $port, $timeout);
	}
	
	
	/**
	 * 
	 * 返回远程 FTP 服务器的操作系统类型
	 * 
	 * @return string
	 */
	public static function systype()
	{
		return ftp_systype(self::$resource);
	}
	
	
	/**
	 * 
	 * 发送一个配置命令远程FTP服务器对上传的文件分配空间。
	 * 注:许多FTP服务器不支持这个命令。
	 *  
	 * @param int $filesize 分配的字节数。
	 * @param string $result 服务器的响应文本表示将通过引用返回的结果如果提供一个变量。
	 * @return bool
	 */
	public static function alloc($filesize,  &$result=&#39;&#39;)
	{
		return ftp_alloc(self::$resource, $filesize, $result);
	}
	
	
	/**
	 * 
	 * 判断是否为目录
	 * 
	 * @param string $remote_file
	 * @return bool
	 */
	public static function isDir($remote_file)
	{
		return (self::size($remote_file) === -1 && self::rawlist($remote_file) !== false) ? true : false;
	}
	
	
	/**
	 * 
	 * 根据文件后缀获取ftp传输模式
	 * 
	 * @param string $file
	 * @return string
	 */
	private static function _getTransferMode($file)
	{
		$extArr = array(&#39;txt&#39;, &#39;text&#39;, &#39;php&#39;, &#39;phps&#39;, &#39;php4&#39;, &#39;js&#39;, &#39;css&#39;, &#39;htm&#39;, &#39;html&#39;, &#39;phtml&#39;, &#39;shtml&#39;, &#39;log&#39;, &#39;xml&#39;);
		if(($pos = strrpos($file, &#39;.&#39;)) === false) return FTP_ASCII;
		$ext = substr($file, $pos + 1);
		return in_array($ext, $extArr) ? FTP_ASCII : FTP_BINARY;
	}
	
	
	/**
	 * 
	 * 上传文件至FTP服务器
	 * 
	 * @param string $local_file
	 * @param string $remote_file
	 * @param int $mode
	 * @param int $permissions
	 * @return void
	 */
	public static function upload($local_file, $remote_file, $mode=null, $permissions = null)
	{
		if(!file_exists($local_file)) throw_exception(&#39;ftp_no_source_file:&#39;.$local_file);
		$mode = is_null($mode) ? self::_getTransferMode($local_file) : $mode;
		if(!self::put($remote_file, $local_file, $mode)) throw_exception("ftp_unable_to_upload:local_file[{$local_file}]/remote_file[{$remote_file}]");
		if(!is_null($permissions)) self::chmod($permissions, $remote_file);
		return true;
	}
	
	
	/**
	 * 
	 * 上传目录至FTP服务器
	 * 注:上传的中目录或文件名不能包含中文
	 * 
	 * @param string $local_dir
	 * @param string $remote_dir
	 * @param int $mode
	 * @param int $permissions
	 */
	public static function uploadDir($local_dir, $remote_dir, $permissions = null)
	{
		$list = Folder::read($local_dir);
		if(count($list) > 0)
		{
			if(!self::isDir($remote_dir)) self::mkdir($remote_dir, $permissions);
			foreach ($list as $k=>$v)
			{
				$local_path = $local_dir.&#39;/&#39;.$v;
				$remote_path = $remote_dir.&#39;/&#39;.$v;
				if(Folder::isDir($local_path)) self::uploadDir($local_path, $remote_path);
				else self::upload($local_path, $remote_path, null, $permissions);
			}
			return true;
		}
		return false;
	}
	
	
	/**
	 * 
	 * 从ftp下载文件至本地
	 * 
	 * @param string $remote_file
	 * @param string $local_file
	 * @param int $mode
	 * @return bool
	 */
	public static function download($remote_file, $local_file, $mode = null)
	{
		$mode = is_null($mode) ? self::_getTransferMode($remote_file) : $mode;
		if(!self::get($local_file, $remote_file, $mode)) throw_exception("ftp_unable_to_download:local_file[{$local_file}]-remote_file[{$remote_file}]");
		return true;
	}
	
	
	/**
	 * 
	 * 下载ftp上的文件夹至本地
	 * 
	 * @param string $remote_dir
	 * @param string $local_dir
	 */
	public static function downloadDir($remote_dir, $local_dir)
	{
		$list = self::nlist($remote_dir);
		if(count($list) > 0)
		{
			if(!Folder::isDir($local_dir)) mk_dir($local_dir);
			foreach ($list as $k=>$v)
			{
				$remote_path = $remote_dir.&#39;/&#39;.$v;
				$local_path = $local_dir.&#39;/&#39;.$v;
				if(self::isDir($remote_path)) self::downloadDir($remote_path, $local_path);
				else self::download($remote_path, $local_path);
			}
			return true;
		}
		return false;
	}
	
	
}



测试代码:
Ftp::connect(&#39;192.168.16.188&#39;, &#39;test&#39;, &#39;123456&#39;);
//$a = Ftp::get(&#39;d:/t.txt&#39;, &#39;php.ini&#39;);
//$a = Date::format(&#39;Y-m-d H:i:s&#39;, Ftp::mdtm(&#39;php.ini&#39;));
//$a = Ftp::mkdir(&#39;maojian&#39;);
//$a = Ftp::nlist(&#39;test&#39;);
//$a = Ftp::pwd();
//$a = Ftp::rawlist(&#39;bak&#39;);
//$a = Ftp::rename(&#39;1111&#39;, &#39;aaa_ftp_test&#39;);
//$a = Ftp::mkdir(&#39;/qq/1111/2222/&#39;);
//$a = Ftp::rmdir(&#39;hahaha&#39;);
//$a = Ftp::systype();
//$a = Ftp::isDir(&#39;aaa_ftp_test/tttt/&#39;);
//$a = Ftp::upload(&#39;d:/aaa.txt&#39;, &#39;aaa.txt&#39;);
//$a = Ftp::delete(&#39;aaa.txt&#39;);
//$a = Ftp::uploadDir(&#39;d:/TDDownload&#39;, &#39;/hahaha&#39;);
//$a = Ftp::downloadDir(&#39;/hahaha&#39;, &#39;d:/aaaa&#39;);
var_dump($a);
登入後複製

                   

 以上就是最新最全的PHP FTP操作类,来自EaglePHP框架的内容,更多相关内容请关注PHP中文网(www.php.cn)!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP的持久相關性:它還活著嗎? PHP的持久相關性:它還活著嗎? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP與Python:了解差異 PHP與Python:了解差異 Apr 11, 2025 am 12:15 AM

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

See all articles