PHP File Read Function
Copy codeThe Code is as follows:
// File Read Function
Function PHP_Read ($ file_name ){
$ Fd = fopen ($ file_name, r );
While ($ bufline = fgets ($ fd, 4096 )){
$ Buf. = $ bufline;
}
Fclose ($ fd );
Return $ buf;
}
?>
File Writing Function
Copy codeThe Code is as follows:
// File Writing Function
Function PHP_Write ($ file_name, $ data, $ method = "w "){
$ Filenum = @ fopen ($ file_name, $ method );
Flock ($ filenum, LOCK_EX );
$ File_data = fwrite ($ filenum, $ data );
Fclose ($ filenum );
Return $ file_data;
}
?>
Static Page generation function
Copy codeThe Code is as follows:
// Static page generation function
Function phptohtm ($ filefrom, $ fileto, $ u2u = 1 ){
If ($ u2u = 1 ){
$ Data = PHP_Read ($ filefrom );
} Else {
$ Data = $ filefrom;
}
PHP_Write ($ fileto, $ data );
Return true;
}
?>
Number of specified conditions
Copy codeThe Code is as follows:
// Query the number of specified conditions
Function rec_exist ($ table, $ where ){
$ Query = "select count (*) as num from $ table". $ where;
$ Result = mysql_query ($ query) or die (nerror (1 ));
$ Rowcount = mysql_fetch_array ($ result );
$ Num = $ rowcount ["num"];
If ($ num = 0 ){
Return false;
}
Return $ num;
}
?>
Directory deletion function
Copy codeThe Code is as follows:
// Directory deletion function
Function del_DIR ($ directory ){
$ Mydir = dir ($ directory );
While ($ file = $ mydir-> read ()){
If (is_dir ("$ directory/$ file") AND ($ file! = ".") AND ($ file! = "..")){
Del_DIR ("$ directory/$ file ");
} Else {
If ($ file! = ".") AND ($ file! = "..")){
Unlink ("$ directory/$ file ");
// Echo "unlink $ directory/$ file OK";
}
}
}
$ Mydir-> close ();
Rmdir ($ directory );
// Echo "rmdir $ directory OK";
}
?>
Directory Name validity check function
Copy codeThe Code is as follows:
// Check the validity of the Directory Name
Function isen ($ str ){
$ Ret = "";
For ($ I = 0; $ I $ p = ord (substr ($ str, $ I, 1 ));
If ($ p <48 & $ p! = 45 & $ p! = 46) | ($ p> 57 & $ p <65) | ($ p> 90 & $ p <97 & $ p! = 95) ||$ p> 122 ){
Nerror ("not compliant with specifications !");
}
$ Ret. = chr ($ p );
}
Return $ ret;
}
?>
PHP paging Functions
Copy codeThe Code is as follows:
// Paging Function
Function splitlist ($ HALT, $ LRLIST, $ ECHOCNT, $ paper, $ table, $ where, $ page_id, $ userid ){
Global $ splitstr, $ sumcnt;
If ($ paper = "" | $ sumcnt = ""){
$ Query = "select count (*) as num from $ table $ where ";
$ Result = mysql_query ($ query );
$ Row = mysql_fetch_array ($ result );
$ Sumcnt = $ row ["num"];
If ($ sumcnt = 0 ){
Nerror ("no news has been selected for this version !");
}
$ Paper = 1;
}
$ Sumpaper = ($ sumcnt-$ sumcnt % $ ECHOCNT)/$ ECHOCNT;
If ($ sumcnt % $ ECHOCNT )! = 0) $ sumpaper + = 1;
If ($ sumpaper = 1 & $ HALT = 0) return ($ where );
$ Enwhere = base64_encode (base64_encode ($ where ));
If ($ LRLIST * 2 + 1) <$ sumpaper ){
If ($ paper-$ LRLIST) <2 ){
$ Tract = 1;
$ Sub = $ LRLIST * 2 + 1;
} Else if ($ paper + $ LRLIST) >=$ sumpaper ){
$ Tract = $ sumpaper-($ LRLIST * 2 );
$ Sub = $ sumpaper;
} Else {
$ Tract = $ paper-$ LRLIST;
$ Sub = $ paper + $ LRLIST;
}
} Else {
$ Tract = 1;
$ Sub = $ sumpaper;
}
$ Uppaper = $ paper-1;
$ Downpaper = $ paper + 1;
$ Startcnt = ($ paper-1) * $ ECHOCNT;
$ Where. = "limit $ {startcnt}, $ {ECHOCNT }";
If ($ tract> 1) {$ splitstr = "[<";}
Else $ splitstr = "[<";
For ($ I = $ tract; $ I <= $ sub; $ I ++ ){
If ($ I! = $ Paper) $ splitstr. = "". $ I. "";
Else $ splitstr. = "". $ I. "";
}
If ($ sub! = $ Sumpaper) $ splitstr. = "> ]";
Else $ splitstr. = "> ]";
Return ($ where );
}
?>
Use of paging functions in PHP
Copy codeThe Code is as follows:
/*
#### Retrieve paging functions ####
Int $ HALT-whether the page number is displayed (1/0) when the search result is divided into only one page
Int $ LRLIST-(number of displayed page numbers-1)/2
Int $ ECHOCNT-number of records displayed per page during retrieval
Int $ paper-page number, pre-extracted: $ paper = $ HTTP_GET_VARS [paper];
Varchar $ table-data table name, with a pre-attached value: $ table = "db. table ";
Varchar $ where-search condition, with a pre-attached value: $ where = "where field = 'value '";
Varchar $ enwhere-encode the original $ where twice and then submit it in GET Mode
Varchar $ splitstr-the output string of the page number. After executing the function, execute echo $ splitstr at the corresponding position;
Get the variable before calling the function-
$ Paper = $ HTTP_GET_VARS [paper];
$ Sumcnt = $ HTTP_GET_VARS [sumcnt];
$ Enwhere = $ HTTP_GET_VARS [enwhere];
Return (Varchar $ where)-Search Condition of the paging query statement
Note: This function calls the error handling function nerror ($ error );
*/
PHP image file upload function
// Image file upload function
Function upload_img ($ UploadFile, $ UploadFile_name, $ UploadFile_size, $ UploadPath, $ max_size = 64 ){
// $ TimeLimit = 60; // set the time-out period. The default time is 30 seconds. If the value is set to 0, the time-out period is not limited.
// Set_time_limit ($ TimeLimit );
If ($ UploadFile! = "None") & ($ UploadFile! = "") {
$ FileName = $ UploadPath. $ UploadFile_name;
If ($ UploadFile_size <1024 ){
$ FileSize = "(string) $ UploadFile_size". "Byte ";
} Elseif ($ UploadFile_size <(1024 * $ max_size )){
$ FileSize = number_format (double) ($ UploadFile_size/1024), 1). "KB ";
} Else {
Nerror ("the file size exceeds the limit !");
}
//{
// $ FileSize = "number_format (double) ($ UploadFile_size"/(1024*1024), 1). "MB ";
//}
If (! File_exists ($ FileName )){
If (copy ($ UploadFile, $ FileName )){
Return "$ UploadFile_name ($ FileSize )";
} Else {
Nerror ("file $ UploadFile_name Upload Failed !");
}
Unlink ($ UploadFile );
} Else {
Nerror ("the file $ UploadFile_name already exists !");
}
// Set_time_limit (30); // restore the default timeout value
}
}
TIPS:
How PHP judges IP address Legality
If (! Strcmp (long2ip (sprintf ("% u", ip2long ($ ip), $ ip) echo "is ipn ";
--
Email Regular Expression judgment
Eregi ("^ [_. 0-9a-zA-Z-] + @ ([0-9a-zA-Z] [0-9a-zA-Z _-] +.) + [a-zA-Z] $", $ email );
Example of checking whether IP addresses and masks are valid
$ Ip = '1970. 168.0.84 ′;
$ Mask = '2017. 255.255.0 ′;
$ Network = '1970. 100 ′;
$ Ip = ip2long ($ ip );
$ Mask = ip2long ($ mask );
$ Network = ip2long ($ network );
If ($ ip & $ mask) = $ network) echo "valid ip and maskn ";
?>
--
How to Set PHP File Download header output
Header ("Content-type: application/x-download ");
Header ("Content-Disposition: attachment; filename = $ file_download_name ;");
Header ("Accept-Ranges: bytes ");
Header ("Content-Length: $ download_size ");
Echo 'xxx'
PHP uses the header to output the ftp download method and supports resumable data transfer.
Example:
Header ('pragma: public ');
Header ('cache-Control: private ');
Header ('cache-Control: no-Cache, must-revalidate ');
Header ('Accept-Ranges: bytes ');
Header ('Connection: close ');
Header ("Content-Type: audio/mpeg ");
Header ("Location: ftp: // download: 1bk3l4s3k9s2@232.2.22.22/2222/web technology development knowledge base/cn_web.rmvb ");
PHP Regular Expression matching (Chinese)
Ereg ("^ [". chr (0xa1). "-". chr (0xff). "] + $", $ str );
Replace hyperlinks in text in batches
Function urlParse ($ str = "){
If ("= $ str) return $ str;
$ Types = array ("http", "ftp", "https ");
$ Replace = <
2.16.htmlentities('1100000000.html entities ('2 ′)."
EOPHP;
$ Ret = $ str;
While (list (, $ type) = each ($ types )){
$ Ret = preg_replace ("| ($ type: //) ([^ s] *) | ie", $ replace, $ ret );
}
Return $ ret;
}