This article mainly introduces the FTP related functions in PHP, involving PHP operation FTP connection, transmission and file and directory related operation skills, with a certain reference value, the need for friends can refer to the next
Specific as follows:
<?phpset_time_limit (0);//dump Local address define (' Store_path ', DirName (__file__). '/.. /.. /.. /.. /temp_data/test/');d efine (' List_path ', Store_path. ' list/');d efine (' Chapter_path ', List_path. ' chapter/');d efine (' 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 _s Erver\n ";} else {echo "couldn ' t connect as $ftp _user\n";} FTP_PASV ($conn _id, true);/** * Create file and write content * @param string $path path/* @param string $fileName fileName * @param strin G $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 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!"; }}}/** * Crawl e-Book category * * @param string $XMLURL XML Address * @param string $saveFileName file name * * @return string return path */funct Ion 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 Htmlgetbookofchapter ($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" \ nthe cache file: ". CreateFile (Store_path, ' bookcache.php ', $arrString);/** * Crawl Book Chapter Info * * @param array $bookIds book chapter Section Information * * @return Boolean returns whether the crawl succeeded */function Getbookofchapter ($bookinfo, $chapters) {if (! Is_array ($chapters)) {retur n 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; 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 $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 ($buf F, "Dirfilter"), foreach ($dirs as $key + $value) {$temp = Preg_split ('/<dir>/', $value); $dirs [$key] = Tri M ($temp [1]);} Asort ($dirs, sort_numeric); return $dirs;} function Dirfilter ($var) {return (Strstr ($var, ' <DIR> '));} Ftp_close ($conn _id);
Summary: The above is the entire content of this article, I hope to be able to help you learn.