首页 > 编程技术 > php

php 缓存技术(1/4)

发布时间:2016-11-25 15:53

关于缓存技术不只在php有,很多系统都有,这是为了减轻服务吕压力与数据库压力来做的,本文章从php缓存技术入门到利用缓存实例来告诉你如何应用php来实例文件缓存描述。

先看这个缓存类

<?php

 代码如下 复制代码

class cache{
/*
class name: cache
description: control to cache data,$cache_out_time is a array to save cache date time out.
version: 1.0
author: 老农 cjjer
last modify:2006-2-26
author url: http://www.111cn.net*/
private $cache_dir;
private $expiretime=180;//缓存的时间是 60 秒
function __construct($cache_dirname){
    if(!@is_dir($cache_dirname)){
        if(!@mkdir($cache_dirname,0777)){
        $this->warn('缓存文件不存在而且不能创建,需要手动创建.');
        return false;
        }
    }
$this->cache_dir    =    $cache_dirname;
}
function __destruct(){
    echo 'cache class bye.';
}

function get_url() {
        if (!isset($_server['request_uri'])) {
                $url = $_server['request_uri'];
        }else{
                $url = $_server['script_name'];
                $url .= (!empty($_server['query_string'])) ? '?' . $_server['query_string'] : '';
        }

        return $url;
}

function warn($errorstring){
echo "<b><font color='red'>发生错误:<pre>".$errorstring."</pre></font></b>";
}

function cache_page($pageurl,$pagedata){
    if(!$fso=fopen($pageurl,'w')){
        $this->warns('无法打开缓存文件.');//trigger_error
        return false;
    }
    if(!flock($fso,lock_ex)){//lock_nb,排它型锁定
        $this->warns('无法锁定缓存文件.');//trigger_error
        return false;
    }
    if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式
        $this->warns('无法写入缓存文件.');//trigger_error
        return false;
    }
    flock($fso,lock_un);//释放锁定
    fclose($fso);
    return true;
}

 

本文章收藏了一款简单php文件上传实例哦,这真的是一款超级简单的文件上传功能代码哦,我们利用php $files来获取要上传文件的,类型,名称与临时名称然后用move_uploaded_file把要上传文件保存到服务器指定目录就OK了。

html代码

 代码如下 复制代码
<input   type= "file "   id= "userfile "   name= "userfile ">
<input   type= "submit "   name= "upload "   value= "上传 ">

处理上传php代码

 

 代码如下 复制代码
<?php
function   do_upload($upload_dir,$upload_url)
    {
          $temp_name   =   $_files[ 'userfile '][ 'tmp_name '];
          $file_name   =   $_files[ 'userfile '][ 'name '];
          $file_name   =   str_replace( "\ ", " ",$file_name);
          $file_name   =   str_replace( " ' ", " ",$file_name);
          $file_path   =   $upload_dir.$file_name;
          $thistime=explode( "- ",date( "y-m-d-h-i-s "));
          $thistime=mktime();
          $filename=$thistime.substr($file_name,strrpos($file_name, ". "));
          //文件名检查
        if($file_name   =   ' ')
            {
                echo   "文件名无效。 ";
                exit;
            }
      if(@move_uploaded_file($_files[ 'userfile '][ 'tmp_name '],$upload_dir.$filename))
          {
                echo   "上传成功。 ";
                echo   " <meta   http-equiv= "refresh "   content= "1;url=www.aimeige.com.cn.php "> ";
                exit;
          }else
            {
                echo   "上传失败。 ";
                echo   " <meta   http-equiv= "refresh "   content= "1;url=www.111cn.net.php "> ";
                exit;
            }
        echo   "end ";
    }
?>
 代码如下 复制代码
调用方法
 代码如下 复制代码
$upload_dir ='down.111cn.net'; $upload_url='www.111cn.net';
 代码如下 复制代码
 do_upload($upload_dir,$upload_url) ;
本文章为你提供一款php简单的伪原创程序哦,如果你正在找伪原创程序这是一款简单的php做的代码哦。

db2word.php

 代码如下 复制代码
<?php
//将数据库教程以数组形式写到文件中
require("conn.php");
$res=mysql教程_query("select k1,k2 from ".table('keywords')." ") ;
$str="<?php ";
while($rs=mysql_fetch_array($res))
{
$str .="$keyword['".$rs[0]."']='".$rs[1]."'; ";
}
$str.="?>";
file_put_contents("keyword.php",$str);
echo "导出成功";
?>

代码二

 代码如下 复制代码
<?php
//将文件中的数组写入到数据库中
require("conn.php");
@require("keyword.php");
mysql_query("delete from ".table('keywords')."");
foreach($keyword as $key=>$val)
{
//$key=iconv('utf-8','gbk',$key);
//$val=iconv('utf-8','gbk',$val);
$pinyin=getfirstchar($key);
$ct=mysql_query("select count(*) from ".table('keywords')." where k1='$key' and k2='$val'");//检测是否已经存在
$ct=@mysql_fetch_array($ct);
$ct=$ct[0];
if($ct<=0)//不存在则插入
{
mysql_query("insert into ".table('keywords')."(k1,k2,pinyin) values('$key','$val','$pinyin')") or die("出错");
}
}
echo "插入成功!";
?>

同意词

提供一款完美的php ajax分页程序哦,如果你正在愁这个就进来看看吧,好了费话不说多喜欢ajax朋友来吧

header("content-type: text/html;charset=gbk");//输出编码,避免中文乱码
?>
<html>
<head>
<title>ajax分页演示</title>
<script language="网页特效" >
var http_request=false;
  function send_request(url){//初始化,指定处理函数,发送请求的函数
    http_request=false;
 //开始初始化xmlhttprequest对象
 if(window.xmlhttprequest){//mozilla浏览器
  http_request=new xmlhttprequest();
  if(http_request.overridemimetype){//设置mime类别
    http_request.overridemimetype("text/xml");
  }
 }
 else if(window.activexobject){//ie浏览器
  try{
   http_request=new activexobject("msxml2.xmlhttp");
  }catch(e){
   try{
   http_request=new activexobject("microsoft.xmlhttp");
   }catch(e){}
  }
    }
 if(!http_request){//异常,创建对象实例失败
  window.alert("创建xmlhttp对象失败!");
  return false;
 }
 http_request.onreadystatechange=processrequest;
 //确定发送请求方式,url,及是否同步执行下段代码
    http_request.open("get",url,true);
 http_request.send(null);
  }
  //处理返回信息的函数
  function processrequest(){
   if(http_request.readystate==4){//判断对象状态
     if(http_request.status==200){//信息已成功返回,开始处理信息
   document.getelementbyid(reobj).innerhtml=http_request.responsetext;
  }
  else{//页面不正常
   alert("您所请求的页面不正常!");
  }
   }
  }
  function dopage(obj,url){
   document.getelementbyid(obj).innerhtml="<font color='green' font-size='12'>正在读取数据...</font>";
   send_request(url);
   reobj=obj;
   }
 
</script>
<style>
/* css教程 document */
#result ul li{
 height:20px;
 width:auto;
 display:block;
 color:#999;
 border:1px solid #999;
 float:left;
 list-style:none;
 font-size:12px;
 margin-left:5px;
 line-height:20px;
 vertical-align:middle;
 text-align:center;
 }
#result ul li a:link{
 width:50px;
 height:20px;
 display:block;
 
 line-height:20px;
 background:#09c;
 border:1px solid #fff;
 color:#fff;
 text-decoration:none;
 }
#result ul li a:hover{
 width:50px;
 height:20px;
 display:block;
 
 line-height:20px;
 background:#09c;
 border:1px solid #fff;
 color:#f60;
 text-decoration:none;
 }
</style>
</head>
<body>
<div id="result">
<?php
$page=isset($_get['page'])?intval($_get['page']):1;        //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
$num=10;                                      //每页显示10条数据

$db=mysql教程_connect("localhost","root","");           //创建数据库教程连接
mysql_select_db("test");                 //选择要操作的数据库

/*
首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,具体的公式就是
总数据库除以每页显示的条数,有余进一。
也就是说10/3=3.3333=4 有余数就要进一。
*/

$result=mysql_query("select * from users");
$total=mysql_num_rows($result); //查询所有的数据

$url='test1.php';//获取本页url

//页码计算
$pagenum=ceil($total/$num);                                    //获得总页数,也是最后一页
$page=min($pagenum,$page);//获得首页
$prepg=$page-1;//上一页
$nextpg=($page==$pagenum ? 0 : $page+1);//下一页
$offset=($page-1)*$num;                                        //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。
$pagenav="<ul>";

//开始分页导航条代码:
$pagenav.="<li>显示第 <b>".($total?($offset+1):0)."</b>-<b>".min($offset+10,$total)."</b> 条记录</li><li>共 $total 条记录&nbsp;</li>";

//如果只有一页则跳出函数:
if($pagenum<=1) return false;

$pagenav.="<li> <a href=javascript:dopage('result','$url?page=1');>首页</a></li> ";
if($prepg) $pagenav.="<li> <a href=javascript:dopage('result','$url?page=$prepg');>前页</a></li> "; else $pagenav.=" <li>前页</li> ";
if($nextpg) $pagenav.="<li><a href=javascript:dopage('result','$url?page=$nextpg');>后页</a> </li>"; else $pagenav.=" <li>后页</li> ";
$pagenav.="<li> <a href=javascript:dopage('result','$url?page=$pagenum');>尾页</a></li> ";
$pagenav.="<li>第 $page 页</li><li>共 $pagenum 页</li></ul>";

//假如传入的页数参数大于总页数,则显示错误信息
if($page>$pagenum){
       echo "error : can not found the page ".$page;
       exit;
}

$info=mysql_query("select * from users limit $offset,$num");   //获取相应页数所需要显示的数据
while($it=mysql_fetch_array($info)){
       echo $it['u_name'];
    echo "<br>";
}                                                              //显示数据
  echo"<br>";
  echo $pagenav;//输出分页导航

?>
</div>
</body>
</html>

<meta http-equiv="content-type" content="text/html; charset=gb2312">

<?php
function tree($directory)
{
$mydir=dir($directory);
echo "<ul> ";
while($file=$mydir->read()){
if((is_dir("$directory/$file")) and ($file!=".") and ($file!=".."))
{echo "<li><font color="#ff00cc"><b>$file</b></font></li> ";
tree("$directory/$file");
}
else{
    $a = explode('.',$file);
    if($a[1] == 'html' || $a[1] == 'htm')
   {
        //unlink($file);
            echo "<li>$file</li> ";
        }   
   

}
}
echo "</ul> ";
$mydir->close();
}
//开始运行

echo "<h2>目录为粉红色</h2><br> ";
tree("./");
?>

标签:[!--infotagslink--]

您可能感兴趣的文章: