This example describes the FTP-related functions in PHP. Share to everyone for your reference, specific as follows:
<?php set_time_limit (0); Dump 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 $ftp _user@ $ftp _server\n";} else {echo "couldn" t
Connect as $ftp _user\n ";
} FTP_PASV ($conn _id, true);
/** * Create file and write to content * @param string $path path/* @param string $fileName fileName * @param string $data content *
* @return String returns the path/function CreateFile ($path, $fileName, $data) {if (empty ($data)) {return false;
} file_put_contents ($path. $fileName, $data); Return $path. $fiLename; /** * Create Directory * * @param string $path path * * @return no/function CreateFolder ($path) {if (! file_exists) ($pat
h)) {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!"; /** * Crawl e-Book category * * @param string $XMLURL XML Address * @param string $saveFileName file name * * @return string back to the road Diameter */function Getclassxml ($conn _id, $categoryDir, $saveFileName = ' category.xml ') {//get top column $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 Column $clist = getdirlistformftp ($conn _id, $categoryDir); Get level Two column category 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 Level Two 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 \ $bookCache =". Var_export ($bookCache, true). "; \
N?> ";
echo \ \ Build cache file:. CreateFile (Store_path, ' bookcache.php ', $arrString); /** * Capture Book Chapter Information * * @param array $bookIds book chapter information * * @return Boolean returns whether to crawl success/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 ']);
//Generate Level Two 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; The 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. '
\ n ', "", $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 ($_boo Klistdir. '
\ n ', ' ", $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, $c
ATEGORYDIR); Gets the taxonomy 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 {i F (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 (@f Tp_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);
For more information on PHP related content readers can view the site topics: "Summary of PHP file operations", "PHP code and transcoding Operation Tips Summary", "PHP object-oriented Program Design Introductory Course", "PHP Mathematical Arithmetic Skills summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, PHP data structure and algorithm tutorial, PHP programming algorithm Summary, PHP Regular Expression usage summary, and PHP Common database operation Skills Summary
I hope this article will help you with the PHP program design.