PHP中FTP相关函数小结


Posted in PHP onJuly 15, 2016

本文实例讲述了PHP中FTP相关函数。分享给大家供大家参考,具体如下:

<?php
set_time_limit(0);
//转存本地地址
define(
  'STORE_PATH',
  dirname(__FILE__) . '/../../../../temp_data/test/'
);
define('LIST_PATH', STORE_PATH . 'list/');
define('CHAPTER_PATH', LIST_PATH . 'chapter/');
define('DETAIL_PATH', LIST_PATH . 'detail/');
createFolder(STORE_PATH);
createFolder(LIST_PATH);
createFolder(CHAPTER_PATH);
createFolder(DETAIL_PATH);
$ftp_server = "ip";
$ftp_user = "anonymous";
$ftp_pass = "anonymous";
$conn_id = ftp_connect ( $ftp_server ) or die ( "Couldn't connect to $ftp_server" );
if (@ftp_login ( $conn_id, $ftp_user, $ftp_pass )) {
echo "Connected as $ftp_user@$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user\n";
}
ftp_pasv($conn_id, true);
/**
 * 创建文件并写入内容
 *
 * @param string $path   path/
 * @param string $fileName filename
 * @param string $data   content
 *
 * @return string 返回路径
 */
function createFile ($path, $fileName, $data)
{
  if (empty($data)) {
    return false;
  }
  file_put_contents($path . $fileName, $data);
  return $path . $fileName;
}
/**
 * 创建目录
 *
 * @param string $path path
 *
 * @return no
 */
function createFolder ($path)
{
  if (! file_exists($path)) {
    createFolder(dirname($path));
    if (mkdir($path, 0777)) {
      echo "\n dir not find ,make dir " . dirname($path) . " is ok!";
    } else {
      echo "\n dir not find ,make dir " . dirname($path) . " is fail!";
    }
  }
}
/**
 * 抓取电子书籍的类别
 *
 * @param string $xmlUrl    xml地址
 * @param string $saveFileName 文件名称
 *
 * @return string 返回路径
 */
function getClassXml ($conn_id,$categoryDir, $saveFileName = 'category.xml')
{
  //获取顶级栏目
$clist = getDirListFormFtp($conn_id, $categoryDir);
$itemTPL = '<Item><ID>%s</ID><Name>%s</Name></Item>';
$items='';
foreach ($clist as $key=>$value){
$items .=sprintf($itemTPL,$value['name'],$value['detail']);
}
$bodyTPL='<Tofo><Class>%s</Class></Tofo>';
$data = sprintf($bodyTPL,$items);
  echo "\n".createFile(STORE_PATH, $saveFileName, $data);
}
set_time_limit(0);
$start_time = time();
$bookCache=array();
$categoryDir = "\\tingshu\\web";
getClassXml($conn_id,$categoryDir);
//获取顶级栏目
$clist = getDirListFormFtp($conn_id, $categoryDir);
//获取二级栏目分类
foreach ($clist as $key=>$value){
$_secondCateoryDir = $categoryDir.'\\'.$value['name'];
$_secondCateorylist = getDirListFormFtp($conn_id, $_secondCateoryDir);
$listData='';
$listTPL='<Tofo><Class><ID>%s</ID><Name>%s</Name><Books>%s</Books></Class></Tofo>';
$items ='<Item><ID>%s</ID><Name>%s</Name></Item>';
$listItemString='';
//获取详细书籍章节列表
foreach ($_secondCateorylist as $key=>$book){
$listItemString.=sprintf($items,$book['name'], $book['detail']);
$_bookListDir = $_secondCateoryDir.'\\'.$book['name'];
$chapters = getBook($conn_id,$_bookListDir);
//生产book章节html
getBookofChapter ($book,$chapters);
//缓存bookid和章节信息
$bookCache[$book['name']] = array('category'=>$value['name'],'chapters'=>$chapters);
}
//生成二级栏目列表页
$listData = sprintf($listTPL, $value['name'], $value['detail'],$listItemString);
$list_save_path = $value['name'] . '.xml';
  echo "\n".createFile(LIST_PATH, $list_save_path, $listData);
}
$arrString = "<?php \n \$bookCache=".var_export($bookCache, true).";\n?>";
echo "\n建立缓存文件:".createFile(STORE_PATH, 'bookcache.php', $arrString);
/**
 * 抓取书籍章节信息
 *
 * @param array $bookIds 书籍章节信息
 *
 * @return boolean 返回是否抓取成功
 */
function getBookofChapter ($bookinfo,$chapters)
{
  if (! is_array($chapters)) {
    return false;
  }
  $bookData = '';
$bookTPL = '<Tofo><Books><ID>%s</ID><Name>%s</Name><Volumes>%s</Volumes></Books></Tofo>';
  $bookItemTPL='<Item><ID>%s</ID><Name>%s</Name></Item>';
  //<Play>%s</Play>
//<Download>%s</Download>
//,$item['downurl'],$item['downurl']
  $chapterString='';
foreach ($chapters as $key=>$item){
  $chapterString.=sprintf($bookItemTPL,$item['name'],$item['detail']);
  }
  //生成二级栏目列表页
$bookData = sprintf($bookTPL, $bookinfo['name'], $bookinfo['detail'],$chapterString);
 $book_chapter_save_path = $bookinfo['name'] . '.xml';
  if (! empty($bookData)) {
    echo "\n".createFile(CHAPTER_PATH, $book_chapter_save_path, $bookData);
  }
  return true;
}
function getBook($conn_id,$_bookListDir){
$chapter=array();
$buff = ftp_nlist ( $conn_id, $_bookListDir );
if(is_array($buff)){
$resourceArray = array();
foreach ($buff as $key=>$value){
if(strstr( $value, '.txt' )){
}else{
$resourcesName = str_replace($_bookListDir.'\\', "", $value);
$temp = preg_split ( '/\./',$resourcesName);
$resourceArray[trim($temp[0])]=$resourcesName;
}
}
foreach ($buff as $key=>$value){
if(strstr( $value, '.txt' )){
$name = trim(str_replace(".txt","",str_replace($_bookListDir.'\\', "", $value)));
$chapter[$name] = array (
'name' => $name,
'detail' => getFileContentsFormFtp ( $conn_id, $value ),
'downurl' =>$resourceArray[$name]
);
}
}
}
ksort($chapter , SORT_NUMERIC);
echo "\n";
var_dump('chapter index :'.implode(array_keys ($chapter), ','));
echo "\n";
return $chapter;
}
function getDirListFormFtp($conn_id, $categoryDir) {
$categoryArray = array ();
$dirs = getDirNameFormFtp ( $conn_id, $categoryDir );
//获取分类描述
foreach ( $dirs as $key => $value ) {
$path = $categoryDir . '\\' . $value . '.txt';
$categoryArray [] = array ('name' => $value, 'detail' => getFileContentsFormFtp ( $conn_id, $path ) );
}
return $categoryArray;
}
function getFileContentsFormFtp($conn_id, $server_file) {
$_tempFileName = STORE_PATH.'temp.tmp';
$content = '';
try {
if (ftp_get ( $conn_id, $_tempFileName, $server_file, FTP_ASCII )) {
$content = file_get_contents ( $_tempFileName );
}
} catch (Exception $e) {
var_dump('error timeout:-----');
global $ftp_server;
$conn_id = ftp_connect ( $ftp_server );
if (@ftp_login ( $conn_id, $ftp_user, $ftp_pass )) {
echo "Connected as $ftp_user@$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user\n";
}
ftp_pasv($conn_id, true);
if (ftp_get ( $conn_id, $_tempFileName, $server_file, FTP_ASCII )) {
$content = file_get_contents ( $_tempFileName );
}
}
$content = iconv ( "GBK", "UTF-8//IGNORE", trim($content) );
echo "\n";
var_dump('file name :'.$server_file.';content:'.$content);
return $content;
}
function getDirNameFormFtp($conn_id, $dirString) {
$buff = ftp_rawlist ( $conn_id, $dirString );
$dirs = array_filter ( $buff, "dirfilter" );
foreach ( $dirs as $key => $value ) {
$temp = preg_split ( '/<DIR>/', $value );
$dirs [$key] = trim ( $temp [1] );
}
asort($dirs , SORT_NUMERIC);
return $dirs;
}
function dirfilter($var) {
return (strstr ( $var, '<DIR>' ));
}
ftp_close ( $conn_id );

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
PHP 函数执行效率的小比较
Oct 17 PHP
PHP计划任务、定时执行任务的实现代码
Apr 23 PHP
解析php中const与define的应用区别
Jun 18 PHP
YII路径的用法总结
Jul 09 PHP
php随机显示指定文件夹下图片的方法
Jul 13 PHP
学习php设计模式 php实现原型模式(prototype)
Dec 07 PHP
yum命令安装php7和相关扩展
Jul 04 PHP
php版阿里云OSS图片上传类详解
Dec 01 PHP
微信公众号模板消息群发php代码示例
Dec 29 PHP
php实现微信企业转账功能
Oct 02 PHP
PHP7引入的&quot;??&quot;和&quot;?:&quot;的区别讲解
Apr 08 PHP
PHP实现简易用户登录系统
Jul 10 PHP
Yii数据模型中rules类验证器用法分析
Jul 15 #PHP
Yii控制器中filter过滤器用法分析
Jul 15 #PHP
Yii编程开发常见调用技巧集锦
Jul 15 #PHP
如何正确配置Nginx + PHP
Jul 15 #PHP
Yii净化器CHtmlPurifier用法示例(过滤不良代码)
Jul 15 #PHP
Yii列表定义与使用分页方法小结(3种方法)
Jul 15 #PHP
session 加入redis的实现代码
Jul 15 #PHP
You might like
PHP+ajax实现二级联动菜单功能示例
2018/08/10 PHP
雄兵连第三季海报曝光,艾妮熙德成主角,蔷薇新造型
2021/03/09 国漫
用JavaScript隐藏控件的方法
2009/09/21 Javascript
JS分页效果示例
2013/10/11 Javascript
jQuery DOM操作实例
2014/03/05 Javascript
JavaScript定义变量和变量优先级问题探讨
2014/10/11 Javascript
JS实现统计复选框选中个数并提示确定与取消的方法
2015/07/01 Javascript
JS基于myFocus库实现各种功能的tab选项卡切换效果
2015/09/19 Javascript
javascript生成随机数方法汇总
2015/11/12 Javascript
JavaScript sort数组排序方法和自我实现排序方法小结
2016/06/06 Javascript
js设置和获取自定义属性的方法
2016/10/20 Javascript
微信小程序 swiper制作tab切换实现附源码
2017/01/21 Javascript
Node.js获取前端ajax提交的request信息
2017/02/20 Javascript
React操作真实DOM实现动态吸底部的示例
2017/10/23 Javascript
Vue.js中关于侦听器(watch)的高级用法示例
2018/05/02 Javascript
vue项目中仿element-ui弹框效果的实例代码
2019/04/22 Javascript
js实现点赞效果
2020/03/16 Javascript
详解Node.js使用token进行认证的简单示例
2020/05/25 Javascript
Vant 在vue-cli 4.x中按需加载操作
2020/11/05 Javascript
[54:47]Liquid vs VP Supermajor决赛 BO 第五场 6.10
2018/07/05 DOTA
浅谈python中的__init__、__new__和__call__方法
2017/07/18 Python
Python抓取框架Scrapy爬虫入门:页面提取
2017/12/01 Python
python 通过logging写入日志到文件和控制台的实例
2018/04/28 Python
django从后台返回html代码的实例
2020/03/11 Python
Python 利用OpenCV给照片换底色的示例代码
2020/08/03 Python
python tkinter实现下载进度条及抖音视频去水印原理
2021/02/07 Python
英国虚拟主机服务商:eUKhost
2016/08/16 全球购物
俄罗斯香水在线商店:AromaCode
2019/12/04 全球购物
会计自我鉴定范文
2013/10/06 职场文书
酒店总经理工作职责
2013/12/13 职场文书
民事诉讼代理词
2015/05/25 职场文书
2016五一手机促销广告语
2016/01/28 职场文书
Django项目配置Memcached和Redis, 缓存选择哪个更有优势
2021/04/06 Python
pytorch中[..., 0]的用法说明
2021/05/20 Python
适合后台管理系统开发的12个前端框架(小结)
2021/06/29 Javascript
Tomcat用户管理的优化配置详解
2022/03/31 Servers