PHP中FTP相關函數總結

來源:互聯網
上載者:User
這篇文章主要介紹了PHP中FTP相關函數,涉及php操作ftp的串連、傳輸及檔案與目錄的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

具體如下:

<?phpset_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\n";} else {echo "Couldn't connect as $ftp_user\n";}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 "\n dir not find ,make dir " . dirname($path) . " is ok!";    } else {      echo "\n 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 "\n".createFile(STORE_PATH, $saveFileName, $data);}set_time_limit(0);$start_time = time();$bookCache=array();$categoryDir = "\\tingshu\\web";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章節htmlgetBookofChapter ($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 "\n".createFile(LIST_PATH, $list_save_path, $listData);}$arrString = "<?php \n \$bookCache=".var_export($bookCache, true).";\n?>";echo "\n建立快取檔案:".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 "\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 );//擷取分類描述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 );

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.