首页 > 编程技术 > php

php fopen实现mysql错误日志记录

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

分享一篇关于php fopen实现mysql错误日志记录用法,因为数据库出错了我们肯定不能把错误日志保存到数据库了,所以我们利用了fopen来记录哦,有需要的朋友参考一下。
 代码如下 复制代码

$time = date("Y-m-d H:i:s");
    $message = $message . "rn$this->sql" . "rn客户IP:$ip" . "rn时间 :$time" . "rnrn";

    $server_date = date("Y-m-d");
    $filename = $server_date . ".txt";
    $file_path = "error/" . $filename;
    $error_content = $message;
    //$error_content="错误的数据库,不可以链接";
    $file = "error"; //设置文件保存目录

    //建立文件夹
    if (!file_exists($file)) {
     if (!mkdir($file, 0777)) {
      //默认的 mode 是 0777,意味着最大可能的访问权
      die("upload files directory does not exist and creation failed");
     }
    }

    //建立txt日期文件
    if (!file_exists($file_path)) {

     //echo "建立日期文件";
     fopen($file_path, "w+");

     //首先要确定文件存在并且可写
     if (is_writable($file_path)) {
      //使用添加模式打开$filename,文件指针将会在文件的开头
      if (!$handle = fopen($file_path, 'a')) {
       echo "不能打开文件 $filename";
       exit;
      }

      //将$somecontent写入到我们打开的文件中。
      if (!fwrite($handle, $error_content)) {
       echo "不能写入到文件 $filename";
       exit;
      }

      //echo "文件 $filename 写入成功";

      echo "——错误记录被保存!";

      //关闭文件
      fclose($handle);
     } else {
      echo "文件 $filename 不可写";
     }

    } else {
     //首先要确定文件存在并且可写
     if (is_writable($file_path)) {
      //使用添加模式打开$filename,文件指针将会在文件的开头
      if (!$handle = fopen($file_path, 'a')) {
       echo "不能打开文件 $filename";
       exit;
      }

      //将$somecontent写入到我们打开的文件中。
      if (!fwrite($handle, $error_content)) {
       echo "不能写入到文件 $filename";
       exit;
      }

      //echo "文件 $filename 写入成功";
      echo "——错误记录被保存!";

      //关闭文件
      fclose($handle);
     } else {
      echo "文件 $filename 不可写";
     }
    }

   }

文章简单的介绍了php中file_get_contents和curl两个函数用法,在不能使用file_get_contents时可以尝试一下curl函数哦,有需要的朋友参考一下。

下面是file_get_contents和curl两个函数同样功能的不同写法

file_get_contents函数的使用示例:

 

 代码如下 复制代码

< ?php

$file_contents = file_get_contents('http://www.111cn.net/');

echo $file_contents;

?>

换成curl函数的使用示例:

 代码如下 复制代码

< ?php

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, 'http://www.111cn.net');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

 

echo $file_contents;

?>


利用function_exists函数来判断php是否支持一个函数可以轻松写出下面函数

 

 代码如下 复制代码

< ?php

function vita_get_url_content($url) {

if(function_exists('file_get_contents')) {

$file_contents = file_get_contents($url);

} else {

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, $url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

}

return $file_contents;

}

?>

其实上面的这个函数还有待商榷,如果你的主机服务商把file_get_contents和curl都关闭了,上面的函数就会出现错误。

一篇关于php分页函数应用及实现原理详解,有需要的同学可以参考一下。

第13课老师提供的分页函数,我进行了适当的修改,去掉了一些我不太好理解的地方。
        只要你好好研究老师的函数,想必很容易就可以判断出我修改的地方。
        如果你觉得我修改有什么不当的地方欢迎指证,交流可以促使你我进步!
    -变量
       1.局部变量
       2.全局变量(global,$GLOBALS)

       a.在函数外定义的局部变量,只可以在主程序使用,不可在函数内或其他文件使用
       b.在函数内定义的局部变量,只可在本函数内使用
       c.在函数外定义的局部变量,想要在函数内使用,必须在函数内将变量声明为global
          
       d.在函数内定义的局部变量,想要在函数外使用,必须在函数内将变量声明为global
          
           注意:
          1.必须先将变量声明为global,然后才赋值
             错误: global $color="red";
             正确: global $color;$color="red";
           2.必须先声明,再使用
             错误:echo $color;global $color;
             正确:global $color;echo $color;
        f.函数的参数为局部变量,如果想要在外部使用,不能使用global这种方式(因为不符合先声明,再使用的原则)
          则使用超全局变量 $GLOBALS(大写,小写有不确定性)
          

 

 

 代码如下 复制代码

<?php
    //$totle为总条数,$displaypg为每页显示的条数
 function PAGEFT($totle, $displaypg = 20) {
  global $firstcount, $pagenav;
  $GLOBALS["displaypg"] = $displaypg;
  //取得当前页数
  if($_GET['page'])
     $page=$_GET['page'];
  else $page = 1;

  //取得当前页的地址
  $url_arr=parse_url($_SERVER["REQUEST_URI"]);
  $url=$url_arr['path'];

  $lastpg = ceil($totle / $displaypg); //最后页
  $page = min($lastpg, $page);        //当前页
  $prepg = $page -1;                 //上一页
  $nextpg = ($page == $lastpg ? 0 : $page +1); //下一页
  $firstcount = ($page -1) * $displaypg;      //开始位置

  //开始分页导航条代码:
  $start=$totle ? ($firstcount +1) : 0;
  $end=min($firstcount + $displaypg, $totle);
  if($start!=$end)
   $pagenav = "显示第 <B>" . $start . "</B>-<B>".$end. "</B> 条记录,共 $totle 条记录";
  else
   $pagenav = "显示第 <B>".$end."</B> 条记录,共 $totle 条记录";

  //如果只有一页则跳出函数:
  if ($lastpg <= 1)
   return TRUE;
  $pagenav .= " <a href='$url'>首页</a> ";
  if ($prepg)
   $pagenav .= " <a href='?page=$prepg'>前页</a> ";
  else
   $pagenav .= " 前页 ";
  if ($nextpg)
   $pagenav .= " <a href='?page=$nextpg'>后页</a> ";
  else
   $pagenav .= " 后页 ";
  $pagenav .= " <a href='?page=$lastpg'>尾页</a> ";

  //下拉跳转列表,循环列出所有页码:
  $pagenav .= " 到第 <select name='topage' size='1' onchange='window.location="$url?page="+this.value'>n";
  for ($i = 1; $i <= $lastpg; $i++) {
   if ($i == $page)
    $pagenav .= "<option value='$i' selected>$i</option>n";
   else
    $pagenav .= "<option value='$i'>$i</option>n";
  }
  $pagenav .= "</select> 页,共 $lastpg 页";
 }
/////////////////////////////////////往下是操作分页函数的部分////////////////////////////////////////////////
include_once('conn.php');
$result=mysql_query("SELECT * FROM `news`");
$total=mysql_num_rows($result);
PAGEFT($total,3);    //调用分页函数

$result=mysql_query("SELECT * FROM `news` limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result)){
echo "<hr><b>".$row['title']." | ".$row['content'];
}
echo "<br>".$pagenav;

?>

一个利用php读出目录下的所有目录及子目录下文件的代码,很简单方便的第一个就是读取一级目录,后面可以无限目录读取有需要的同学可以参考一下。
 代码如下 复制代码

<?php
$dirname=@opendir("abc")or die("打开目录不成功<br>");

readdir($dirname)."<br>";

readdir($dirname)."<br>";
while(($filess=readdir($dirname))!=false)

{
  if(is_dir("abc/".$filess))
 {
 
    echo "目录:".$filess."<br>";

    }
   else
 {
  
     echo "文件:".$filess."<br>";
   }

}
closedir($dirname);

?>

只能读出html文件名,读不出abc目录下的目录名,下面我们再看一实例

 代码如下 复制代码

<?php 
/*
*   递归获取指定路径下的所有文件或匹配指定正则的文件(不包括“.”和“..”),结果以数组形式返回
*   @param  string  $dir
*   @param  string  [$pattern]
*   @return array
*/ 
function file_list($dir,$pattern="") 

    $arr=array(); 
    $dir_handle=opendir($dir); 
    if($dir_handle) 
    { 
        // 这里必须严格比较,因为返回的文件名可能是“0”  
        while(($file=readdir($dir_handle))!==false) 
        { 
            if($file==='.' || $file==='..') 
            { 
                continue; 
            } 
            $tmp=realpath($dir.'/'.$file); 
            if(is_dir($tmp)) 
            { 
                $retArr=file_list($tmp,$pattern); 
                if(!emptyempty($retArr)) 
                { 
                    $arr[]=$retArr; 
                } 
            } 
            else 
            { 
                if($pattern==="" || preg_match($pattern,$tmp)) 
                { 
                    $arr[]=$tmp; 
                } 
            } 
        } 
        closedir($dir_handle); 
    } 
    return $arr; 

 
// 列出网站根目录下所有以".php"扩展名(不区分大小写)结尾的文件  
echo '<pre>'; 
print_r(file_list($_SERVER['DOCUMENT_ROOT'],"//.php$/i")); 
echo '</pre>'; 
?> 
这个就可以方便的无限目录遍历了。

主要是url操作函数,可以删除字符串的url同时也可以给字符串中的url加连接哦,有需要的朋友参考一下。

删除url

 

 代码如下 复制代码
$string = preg_replace('/b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]/i', '', $string);

将url转换成链接

 

 代码如下 复制代码
$url = "Jean-Baptiste Jung (http://www.111cn.net)";
$url = preg_replace("#http://([A-z0-9./-]+)#", '<a href="http://www.111cn.net $1" style="font-size: 12px; vertical-align: baseline; background-color: transparent; margin: 0px; padding: 0px; color: #3777af; text-decoration: none; font-weight: bold">$0</a>', $url);
标签:[!--infotagslink--]

您可能感兴趣的文章: