php常用技巧总结

php文件读取函式
//文件读取函式
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;
}
?>
静态页面生成函式
//静态页面生成函式
function phptohtm($filefrom,$fileto,$u2u=1){
if($u2u==1){
$data=php_read($filefrom);
}else{
$data=$filefrom;
}
php_write($fileto,$data);
return true;
}
?>
指定条件信息数量检索函式
//指定条件信息数量检索函式
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 “;
}
?>
目录名称合法性检测函式
//目录名称合法性检测
function isen($str){
$ret=””;
for($i=0;$i $p=ord(substr($str,$i,1));
if(($p57 & $p90 & $p122){
nerror(“不符合规范!”);
}
$ret.=chr($p);
}
return $ret;
}
?>
php分页函数
//分页函数
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(“该版内还没有选择发布新闻 !”);
}
$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=”【 > 】”;
return($where);
}
?>
php关于分页函式的使用说明
php图片文件上传函式
//图片文件上传函式
function upload_img($uploadfile,$uploadfile_name,$uploadfile_size,$uploadpath,$max_size=64){
//$timelimit=60; //设置超时限制时间 缺省时间为 30秒 设置为0时为不限时
//set_time_limit($timelimit);
if(($uploadfile!= “none” )&&($uploadfile != “” )){
$filename=$uploadpath.$uploadfile_name;
if($uploadfile_size

Posted in 未分类

发表评论