PHP中FTP相关函数小结
程序员文章站
2024-04-02 16:32:34
本文实例讲述了php中ftp相关函数。分享给大家供大家参考,具体如下:
本文实例讲述了php中ftp相关函数。分享给大家供大家参考,具体如下:
<?php set_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章节html getbookofchapter ($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 );
更多关于php相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《php编码与转码操作技巧汇总》、《php面向对象程序设计入门教程》、《php数学运算技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
下一篇: 教你在header中隐藏php的版本信息