Home>
<?php
set_time_limit (0);
//dump the local address
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 [email protected]$ftp_server \ n";
} else {
echo "couldn" t connect as $ftp_user \ n ";
}
ftp_pasv ($conn_id, true);
/**
 * Create files and write content
 *
 * @param string $path path /
 * @param string $filename filename
 * @param string $data content
 *
 * @return string return path
 * /
function createfile ($path, $filename, $data)
{
  if (empty ($data)) {
    return false;
  }
  file_put_contents ($path. $filename, $data);
  return $path. $filename;
}
/**
 * Create a directory
 *
 * @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!";
    }
  }
}
/**
 * Grab e-book categories
 *
 * @param string $xmlurl xml address
 * @param string $savefilename file name
 *
 * @return string return path
 * /
function getclassxml ($conn_id, $categorydir, $savefilename="category.xml")
{
  //Get top-level columns
$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);
//Get top-level columns
$clist=getdirlistformftp ($conn_id, $categorydir);
//Get secondary column classification
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="";
//Get detailed book chapter list
foreach ($_secondcateorylist as $key =>$book) {
$listitemstring.=sprintf ($items, $book ["name"], $book ["detail"]);
$_booklistdir=$_secondcateorydir. "\\". $book ["name"];
$chapters=getbook ($conn_id, $_ booklistdir);
//production book chapter html
getbookofchapter ($book, $chapters);
//Cache bookid and chapter information
$bookcache [$book ["name"]]=array ("category" =>$value ["name"], "chapters" =>$chapters);
}
//Generate secondary column list page
$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 "\ nCreate a cache file:". createfile (store_path, "bookcache.php", $arrstring);
/**
 * Grab book chapter information
 *
 * @param array $bookids book chapter information
 *
 * @return boolean returns whether the fetch is successful
 * /
function getbookofchapter ($bookinfo, $chapters)
{
  if (! is_array ($chapters)) {
    return false;
  }
  $bookdata="";
$booktpl="<tofo><books><id>%s</id><name>%s</name>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"]);
  }
  //Generate secondary column list page
$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);
//Get classification description
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 [email protected]$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
  • Previous Yii CActiveForm-based Ajax data validation usage example
  • Next Visual Studio 2015 and NET Core installation tutorial