Summary of FTP-related functions and phpftp functions in PHP
This example describes FTP-related functions in PHP. We will share this with you for your reference. The details are as follows:
<? Phpset_time_limit (0); // locally stored 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 = "anonym Ous "; $ 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 a file and write the content ** @ param string $ path/* @ param string $ fileName filename * @ param string $ data content ** @ return string return path * /funct Ion createFile ($ path, $ fileName, $ data) {if (empty ($ data) {return false;} file_put_contents ($ path. $ fileName, $ data); return $ path. $ fileName;}/*** create directory ** @ param string $ 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! ";}}} /*** Category of the captured e-books ** @ 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 ['det Ail ']);} $ 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 ); // obtain the second-level topic 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 = ''; // obtain the detailed book chapter list foreach ($ _ secondCateorylist as $ key => $ book) {$ listItemString. = sprintf ($ items, $ book ['name'], $ book ['detail']); $ _ bookListDir = $ _ secon DCateoryDir. '\\'. $ book ['name']; $ chapters = getBook ($ conn_id, $ _ bookListDir); // generate the htmlgetBookofChapter ($ book, $ chapters) of the book chapter ); // cache bookid and Chapter Information $ bookCache [$ book ['name'] = array ('category '=> $ value ['name'], 'chapters '=> $ chapters);} // generate a list of second-level columns 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" \ n create a cache file :". createFile (STORE_PATH, 'bookcache. php', $ arrString ); /*** capture book chapter information ** @ param array $ bookIds book chapter information ** @ return boolean returns whether the collection is successful */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 the list of second-level columns 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); // obtain the category 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 ($ 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 );