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='') { 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('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'); if(($pos = strrpos($file, '.')) === 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('ftp_no_source_file:'.$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.'/'.$v; $remote_path = $remote_dir.'/'.$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.'/'.$v; $local_path = $local_dir.'/'.$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('192.168.16.188', 'test', '123456'); //$a = Ftp::get('d:/t.txt', 'php.ini'); //$a = Date::format('Y-m-d H:i:s', Ftp::mdtm('php.ini')); //$a = Ftp::mkdir('maojian'); //$a = Ftp::nlist('test'); //$a = Ftp::pwd(); //$a = Ftp::rawlist('bak'); //$a = Ftp::rename('1111', 'aaa_ftp_test'); //$a = Ftp::mkdir('/qq/1111/2222/'); //$a = Ftp::rmdir('hahaha'); //$a = Ftp::systype(); //$a = Ftp::isDir('aaa_ftp_test/tttt/'); //$a = Ftp::upload('d:/aaa.txt', 'aaa.txt'); //$a = Ftp::delete('aaa.txt'); //$a = Ftp::uploadDir('d:/TDDownload', '/hahaha'); //$a = Ftp::downloadDir('/hahaha', 'd:/aaaa'); var_dump($a);
以上就是最新最全的PHP FTP操作类,来自EaglePHP框架的内容,更多相关内容请关注PHP中文网(www.php1.cn)!