很有用的文件操作php函數(shù)_PHP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:解讀php生成靜態(tài)頁面的簡(jiǎn)單實(shí)例一個(gè)簡(jiǎn)單的實(shí)例: 新聞模版文件news_tmp.html: html head title{title}/title meta http-equiv=Content-Type content=text/html; charset=gb2312 /head body TABLE border=0 width=767 cellspacing=0 cellpadding=5 TR TD div align=center{news_ti
文件讀取函式//文件讀取函式
function PHP_Read(file_name) {
fd=fopen(file_name,r);
while(bufline=fgets(fd, 4096)){
buf.=bufline;
}
fclose(fd);
return buf;
}
?>
文件寫入函式
//文件寫入函式
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;
}
?>
靜態(tài)頁面生成函式
//靜態(tài)頁面生成函式
function phptohtm(filefrom,fileto,u2u=1){
if(u2u==1){
data=PHP_Read(filefrom);
}else{
data=filefrom;
}
PHP_Write(fileto,data);
return true;
}
?>
指定條件信息數(shù)量檢索函式
//指定條件信息數(shù)量檢索函式
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;
}
?>
目錄刪除函式
//目錄刪除函式
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
";
}
?>
目錄名稱合法性檢測(cè)函式
//目錄名稱合法性檢測(cè)
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("不符合規(guī)范!");
}
ret.=chr(p);
}
return ret;
}
?>
分頁函式
//分頁函式
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("該版內(nèi)還沒有選擇發(fā)布新聞 !");
}
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);
}
?>
關(guān)于分頁函式的使用說明
/*
#### 檢索分頁函式 ####
Int HALT - 檢索結(jié)果僅分1頁時(shí)是否(1/0)顯示頁碼條
Int LRLIST - (頁碼條顯示頁碼數(shù)-1)/2
Int ECHOCNT - 檢索時(shí)每頁顯示記錄的數(shù)量
Int paper - 頁數(shù),預(yù)提�。簆aper=HTTP_GET_VARS[paper];
Varchar table - 數(shù)據(jù)表名,預(yù)附值:table="db.table";
Varchar where - 檢索條件,預(yù)附值:where="where field=@#value@#";
Varchar enwhere - 將原where進(jìn)行兩次base64_encode()編碼后以GET的方式提交
Varchar splitstr - 頁碼條輸出字串,執(zhí)行函式后在相應(yīng)的位置執(zhí)行 echo splitstr;
函式調(diào)用前需獲取變量 -
paper=HTTP_GET_VARS[paper];
sumcnt=HTTP_GET_VARS[sumcnt];
enwhere=HTTP_GET_VARS[enwhere];
Return (Varchar where) - 分頁后檢索語句的檢索條件
注意:本函式需調(diào)用出錯(cuò)處理函式 nerror(error);
*/
?>
圖片文件上傳函式
//圖片文件上傳函式
function upload_img(UploadFile,UploadFile_name,UploadFile_size,UploadPath,max_size=64){
//TimeLimit=60; //設(shè)置超時(shí)限制時(shí)間 缺省時(shí)間為 30秒 設(shè)置為0時(shí)為不限時(shí)
//set_time_limit(TimeLimit);
if((UploadFile!= "none" )&&(UploadFile != "" )){
FileName=UploadPath.UploadFile_name;
if(UploadFile_size <1024){
FileSize="(string)UploadFile_size" . "字節(jié)";
}elseif(UploadFile_size <(1024 * max_size)){
FileSize=number_format((double)(UploadFile_size / 1024), 1) . " KB";
}else{
nerror("文件超過限制大小!");
}
//{
//FileSize="number_format((double)(UploadFile_size" / (1024 * 1024)), 1) . " MB";
// }
if(!file_exists(FileName)){
if(copy(UploadFile,FileName)){
return "UploadFile_name (FileSize)";
}else{
nerror("文件 UploadFile_name 上載失��!");
}
unlink(UploadFile);
}else{
nerror("文件 UploadFile_name 已經(jīng)存在!");
}
//set_time_limit(30); //恢復(fù)缺省超時(shí)設(shè)置
}
}
?>
分享:如何讓PHP支持?jǐn)帱c(diǎn)續(xù)傳文件現(xiàn)在的很多服務(wù)器都支持?jǐn)帱c(diǎn)續(xù)傳去下載軟件,同時(shí)很多下載軟件也是斷點(diǎn)續(xù)傳,怎么樣才能讓PHP也能實(shí)現(xiàn)斷電續(xù)傳功能呢?請(qǐng)先看下面的代碼。 fname = './05e58c19552bb26b158f6621a6650899'; fp = fopen(fname,'rb'); fsize = filesize(fname); if (isset(_SER
相關(guān)PHP教程:
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-很有用的文件操作php函數(shù)
。