直接可以拿來用的PHP常用功能程式碼片段(6~10)
文章來源:jquery教程?-?http://www.jq-school.com/Show.aspx?id=324
?
前面已經分享了PHP常用功能程式碼片段(1~5),今天是第二篇,也就是第六到十這5個實現程式碼片段,希望可以幫到jquery學堂群裡面的成員和廣大對PHP開發的網友們提高開發效率,以下是第二篇文章。
6、PHP實現zip壓縮解壓通用函數
function ezip($zip, $hedef = ''){ $dirname=preg_replace('/.zip/', '', $zip); $root = $_SERVER['DOCUMENT_ROOT'].'/zip/'; // echo $root. $zip; $zip = zip_open($root . $zip); // var_dump($zip); @mkdir($root . $hedef . $dirname.'/'.$zip_dosya); while($zip_icerik = zip_read($zip)){ $zip_dosya = zip_entry_name($zip_icerik); if(strpos($zip_dosya, '.')){ $hedef_yol = $root . $hedef . $dirname.'/'.$zip_dosya; @touch($hedef_yol); // echo $hedef_yol; $yeni_dosya = @fopen($hedef_yol, 'w+'); @fwrite($yeni_dosya, zip_entry_read($zip_icerik)); @fclose($yeni_dosya); // $yeni_dosya; }else{ @mkdir($root . $hedef . $dirname.'/'.$zip_dosya); // echo $root . $hedef . 'x/'.$zip_dosya; }; };}// ezip('yuol.zip','./tr/');function zip($path) { $path=preg_replace('/\/$/', '', $path); preg_match('/\/([\d\D][^\/]*)$/', $path, $matches, PREG_OFFSET_CAPTURE); $filename=$matches[1][0].".zip"; // var_dump($filename); // set_time_limit(0); $zip = new ZipArchive(); $zip->open($filename,ZIPARCHIVE::OVERWRITE);//return ; // var_dump($path); if (is_file($path)) { $path=preg_replace('/\/\//', '/', $path); $base_dir=preg_replace('/\/[\d\D][^\/]*$/', '/', $path); $base_dir=addcslashes($base_dir, '/:'); $localname=preg_replace('/'.$base_dir.'/', '', $path); // var_dump($localname); $zip->addFile($path,$localname); // var_dump($path); $zip->close(); return; }elseif (is_dir($path)) { $path=preg_replace('/\/[\d\D][^\/]*$/', '', $path); $base_dir=$path.'/';//基目錄 $base_dir=addcslashes($base_dir, '/:'); // var_dump($base_dir); } $path=preg_replace('/\/\//', '/', $path); // var_dump($path); function addItem($path,&$zip,&$base_dir){ // var_dump($path); $handle = opendir($path); // var_dump($path); while (false !== ($file = readdir($handle))) { if (($file!='.')&&($file!='..')){ // var_dump($file); $ipath=$path.'/'.$file; if (is_file($ipath)){//條目是檔案 $localname=preg_replace('/'.$base_dir.'/', '', $ipath); var_dump($localname); $zip->addFile($ipath,$localname); // var_dump($r); } else if (is_dir($ipath)){ addItem($ipath,$zip,$base_dir); $localname=preg_replace('/'.$base_dir.'/', '', $ipath); var_dump($localname); $zip->addEmptyDir($localname); } // var_dump($path); } } } // var_dump($base_dir); addItem($path,$zip,$base_dir); $zip->close();}//調用方法zip('解壓的目錄');
7、php實用擷取遠程圖片的通用方法
function auto_save_image($body){$img_array = explode('&',$body);/*$img_array = array();preg_match_all("/(src)=[\"|\'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))[\"|\'| ]{0,}/isU", $body, $img_array);$img_array = array_unique($img_array[2]);*/ //也可以自動匹配set_time_limit(0);$imgPath = "uploads/allimg/".date("Ymd")."/";$milliSecond = strftime("%H%M%S",time());if(!is_dir($imgPath)) @mkdir($imgPath,0777);foreach($img_array as $key =>$value){$value = trim($value);$get_file = @file_get_contents($value);$rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);if($get_file){$fp = @fopen($rndFileName,"w");@fwrite($fp,$get_file);@fclose($fp);}$body = @ereg_replace($value, $rndFileName, $body);} return $body;}
8、PHP實現簡單的檔案下載通用方法
function download_file($file){if(is_file($file)){$length = filesize($file);$type = mime_content_type($file);$showname = ltrim(strrchr($file,'/'),'/');header("Content-Description: File Transfer");header('Content-type: ' . $type);header('Content-Length:' . $length);if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');} else { header('Content-Disposition: attachment; filename="' . $showname . '"');}readfile($file);exit;} else { exit('檔案已被刪除!');}}
9、PHP實現匯出Excel檔案通用方法
/** * 匯出資料為excel表格 *@param $data 一個二維數組,結構如同從資料庫查出來的數組 *@param $title excel的第一列名,一個數組,如果為空白則沒有標題 *@param $filename 下載的檔案名稱 *@examlpe $stu = M ('User'); $arr = $stu -> select(); exportexcel($arr,array('id','賬戶','密碼','暱稱'),'檔案名稱!');*/function exportexcel($data=array(),$title=array(),$filename='report'){ header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=".$filename.".xls"); header("Pragma: no-cache"); header("Expires: 0"); //匯出xls 開始 if (!empty($title)){ foreach ($title as $k => $v) { $title[$k]=iconv("UTF-8", "GB2312",$v); } $title= implode("\t", $title); echo "$title\n"; } if (!empty($data)){ foreach($data as $key=>$val){ foreach ($val as $ck => $cv) { $data[$key][$ck]=iconv("UTF-8", "GB2312", $cv); } $data[$key]=implode("\t", $data[$key]); } echo implode("\n",$data); }}
10、PHP實現如何清除TP的檔案快取
public function cache_clear() {$this->deldir(TEMP_PATH);}function deldir($dir) {$dh = opendir($dir);while ($file = readdir($dh)) {if ($file != "." && $file != "..") {$fullpath = $dir . "/" . $file;if (!is_dir($fullpath)) {unlink($fullpath);} else {deldir($fullpath);}}}}
?