直接可以拿來用的PHP慣用功能程式碼片段(6~10)

來源:互聯網
上載者:User
直接可以拿來用的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);}}}}

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.