巅云智能建站平台搭建版(创业流派版)火爆上线,毕生受权!新增:文章智能收罗+全站真静态打包+都会分站+智能小法式+不法词过滤+H5自顺应+智能链词等功效功效概况
赞助文档Help

PHP中FTP相干函数小结

一佰互联网站建造(www.taishanly.com) 宣布日期 2020-04-27 10:44:09 阅读数: 120

本文实例报告了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
";
} else {
echo "Couldn"t connect as $ftp_user
";
}
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 "
 dir not find ,make dir " . dirname($path) . " is ok!";
    } else {
      echo "
 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 "
".createFile(STORE_PATH, $saveFileName, $data);
}
set_time_limit(0);
$start_time = time();
$bookCache=array();
$categoryDir = "tingshuweb";
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 "
".createFile(LIST_PATH, $list_save_path, $listData);
}
$arrString = "<?php 
 $bookCache=".var_export($bookCache, true).";
?>";
echo "
成立缓存文件:".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 "
".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 "
";
var_dump("chapter index :".implode(array_keys ($chapter), ","));
echo "
";
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
";
} else {
echo "Couldn"t connect as $ftp_user
";
}
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 "
";
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编码与转码操纵技能汇总》、《php面向工具法式设想入门教程》、《PHP数学运算技能总结》、《PHP数组(Array)操纵技能大全》、《php字符串(string)用法总结》、《PHP数据布局与算法教程》、《php法式设想算法总结》、《php正则抒发式用法总结》、及《php罕见数据库操纵技能汇总》

但愿本文所述对大师PHP法式设想有所赞助。

一佰互联是天下着名建站品牌办事商,咱们有九年、网站建造、网页设想、php开辟和域名注册及假造主机办事经历,供给的办事更是天下着名。最近几年来还整合团队上风自立开辟了可视化多用户”“3.0平台版,拖拽排版网站建造设想,轻松完成pc站、手机微网站、小法式、APP一体化全网营销网站扶植 ,已胜利的为天下上百家收集公司供给自助建站平台搭建办事。

相干消息more

09
04月
用3个实在案例,告知你若何挑选合适本身的任务

本章节咱们经由过程三个实在案例来深度阐发若何找到合适本身的任务,外行业中稳步进步,不时进步,终究成为一位优异的设想师,取得行业承认,完成自我代价... >>概况

26
10月
网站建造需到达哪些请求更受喜爱

有目标性去建造企业网站,那末规范天然会高。想要操纵网站到达本身的目标,在这方面投入的精神也不会少。好网站须要有特定的请求,如斯能力吸收更多的... >>概况

16
04月
专业网站扶植-90后收集写手身家万万?收集发

克日,海角服装服装服装服装服装论坛t.vhao.nett.vhao.nett.vhao.nett.vhao.nett.vhao.net上一则表妹为表哥征婚的帖子吸收了大师,帖子中称,“表哥”是个90后,湖北仙桃市人,诞生乡村,是一位收集... >>概况

18
04月
医疗行业怎样做好网站扶植?建站注重事变及经历

医疗行业作为一个特别、敏感的行业,做好网站扶植是一件很不轻易的工作。跟着互联网的高速成长,医疗行业在做好线下医疗办事的同时,也垂垂将眼光投向... >>概况

高端网站扶植

美工统筹SEO,为企业电子商务营销助力!

德律风:

023-85725751
建站

产物

域名注册 假造主机 云办事器 企业邮局
智能建站 APP打包 微站/小法式 创业平台
网站推行 媒体营销 智能收罗 AI机械人
400德律风 短信营销 店销机械人
私家定制 流派网站