Several useful functions for learning PHP dynamic web skills collection

Source: Internet
Author: User
Some useful functions can be retained as source code to avoid repeated writing in the future. File reading function // file reading function functionPHP_Read ($ file_name) {$ fd = fopen ($ file_name, r); while ($ bufline = fgets ($ fd, 4096 )) {$ buf. = $ bufline ;}

Some useful functions can be retained as source code to avoid repeated writing in the future.

File reading function

// File reading 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

// Write the file into the 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 native functions

// Static page native 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 condition information retrieval functions

// Search 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

// 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

// 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 ('noncompliant! ');
}
$ Ret. = chr ($ p );
}
Return $ ret;
}
?>


Paging functions

// Paging function
Function splitlist ($ HALT, $ LRLIST, $ ECHOCNT, $ ***, $ table, $ where, $ page_id, $ userid ){
Global $ splitstr, $ sumcnt;
If ($ *** = ''| $ 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 ('The announcement message has not been selected in this version! ');
}
$ *** = 1;
}
$ Sum *** = ($ sumcnt-$ sumcnt % $ ECHOCNT)/$ ECHOCNT;
If ($ sumcnt % $ ECHOCNT )! = 0) $ sum *** = 1;
If ($ sum *** = 1 & $ HALT = 0) return ($ where );
$ Enwhere = base64_encode (base64_encode ($ where ));
If ($ LRLIST * 2 1) <$ sum ***){
If ($ ***-$ LRLIST) <2 ){
$ Tract = 1;
$ Sub = $ LRLIST * 2 1;
} Else if ($ *** $ LRLIST) >=$ sum ***){
$ Tract = $ sum ***-($ LRLIST * 2 );
$ Sub = $ sum ***;
} Else {
$ Tract = $ ***-$ LRLIST;
$ Sub = $ *** $ LRLIST;
}
} Else {
$ Tract = 1;
$ Sub = $ sum ***;
}
$ Up *** = $ ***-1;
$ Down *** = $ *** 1;
$ Startcnt = ($ ***-1) * $ ECHOCNT;
$ Where. = 'limit $ {startcnt}, $ {ECHOCNT }';
If ($ tract> 1) {$ splitstr = '[<';}
Else $ splitstr = '[<';
For ($ I = $ tract; $ I <= $ sub; $ I ){
If ($ I! = $ ***) $ Splitstr. = ''. $ I .'';
Else $ splitstr. = ''. $ I .'';
}
If ($ sub! = $ Sum ***) $ splitstr. = '> ]';
Else $ splitstr. = '> ]';
Return ($ where );
}
?>

Application clarification of paging functions

/*
#### Retrieve paging functions ####
Int $ HALT-whether the page number is displayed (1/0) when only one page is displayed for the search result
Int $ LRLIST-(number of displayed page numbers-1)/2
Int $ ECHOCNT-number of records displayed per page during retrieval
Int $ ***-number of pages, pre-extracted: $ *** = $ HTTP_GET_VARS [***];
Varchar $ table-name of the data table, with the following values: $ table = 'DB. table ';
Varchar $ where-search condition, with a value prefixed: $ where = 'Where field = 'value '';
Varchar $ enwhere-encode the original $ where twice and then submit it in the GET method
Varchar $ splitstr-the output string of the page number. after the function is executed, echo $ splitstr is executed in the corresponding position;


Get the variable before calling the function-
$ *** = $ HTTP_GET_VARS [***];
$ Sumcnt = $ HTTP_GET_VARS [sumcnt];
$ Enwhere = $ HTTP_GET_VARS [enwhere];
Return (Varchar $ where)-Search condition of the paging query statement
Note: This function must call the error handling function nerror ($ error );
*/
?>


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 ('file $ UploadFile_name already exists! ');
}
// Set_time_limit (30); // restore the default timeout value
}
}
?>


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.