PHP Usage Code snippet (6~10) that can be used directly

Source: Internet
Author: User
Tags ziparchive
PHP Common function Code snippet (6~10) that can be used directly

Article Source: jquery tutorial?-? http://www.jq-school.com/Show.aspx?id=324

?

I 've already shared the previous PHP Common function code Snippet (*) , today is the second, sixth to tenth of these 5 implementation code snippets, hoping to help jquery the members of the school group and the vast majority of PHP development of netizens to improve the efficiency of development, the following is the second article.

6, PHP implementation zip compression decompression General function

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 directory $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)) {//entry is a file $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 ();} Call method Zip (' unzipped directory ');


7, PHP practical access to remote images of the general method

function Auto_save_image ($body) {$img _array = explode (' & ', $body);/* $img _array = array ();p Reg_match_all ("/(SRC) =[ \"|\'| ]{0,} (http:\/\/(. *) \. ( gif|jpg|jpeg|bmp|png)) [\ "|\ ' |] {0,}/isu ", $body, $img _array), $img _array = Array_unique ($img _array[2]), *///can also automatically match 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 implementation of simple File download general method

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 (' file has been deleted! ');}}


9, PHP implementation of the common method of exporting Excel files

/** * Export data to Excel table * @param $data a two-dimensional array, structured like an array from a database * @param $title the first row of Excel header, an array, if empty, no title * @param    $filename the downloaded file name * @examlpe $stu = M (' User ');    $arr = select ($stu); Exportexcel ($arr, array (' ID ', ' account ', ' Password ', ' nickname '), ' filename! '); */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"); Export xls start 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 implementation of how to clear the file cache 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);}}}

?

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.