Common PHP function code snippets that can be used directly (6 ~ 10)

Source: Internet
Author: User
Common PHP function code snippets that can be used directly (6 ~ 10) Source: jquery tutorial? -? Http://www.jq-school.com/Show.aspx? Id = 324? The code snippet of common PHP functions (1 ~ 5) today is the second article, which is the code snippet of common PHP functions that can be directly used by the five instances from the sixth to tenth (6 ~ 10)

Source: jquery tutorial? -? Http://www.jq-school.com/Show.aspx? Id = 324

?

I have already shared it.Common PHP function code snippets (1 ~ 5)Today is the second article, that is, the 5 implementation code snippets from the sixth to the tenth.JqueryMembers of the school group andPHPDeveloped netizens improve development efficiency. The following is the second article.

6. PHP universal functions for zip compression and decompression

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_dosy A; @ 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_repl Ace ('/'. $ 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, & $ B Ase_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) {// The entry is the 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 the method zip ('decompressed directory ');


7. common php method for obtaining remote images

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]); * // You 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. simple file download methods in 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 ($ sh Owname ). '"');} else {header ('content-Disposition: attachment; filename = "'. $ showname. '"');} readfile ($ file); exit;} else {exit ('The file has been deleted! ');}}


9. general methods for exporting Excel files using PHP

/*** Export data into an excel table * @ param $ data: A two-dimensional array. The structure is like the array found from the database * @ param $ title: the first row title of excel, an array, if it is null, no title * @ param $ filename indicates the downloaded file name * @ examlpe $ stu = M ('user'); $ arr = $ stu-> select (); exportexcel ($ arr, array ('id', 'account', 'password', 'Nickname '), 'File name! '); */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 to 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 implements how to clear the TP file cache

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);}}}}

?

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.