易截截图软件、单文件、免安装、纯绿色、仅160KB

PHP实现类似tail命令读取最后n行的方法

原文出自: http://www.hly1980.cn/archives/118.html
需要分析日志时tail命令可是常需要用到的,可惜php内并没有提供类似的方法,所以自己实现了一个。
调用方式为tail($filename, $rows),每次默认读取1024字节作为缓冲,返回字符串数组,文件尾的行靠前(这里的行为和tail有所区别,如果需要以原序返回的请自行调用array_reverse)。
代码如下:
/**
* 读取文件最后若干行的数据
*
* @param string $filename
* 文件名
* @param string $rows
* 行数
* @param string $size
* 内存缓冲区大小,默认为1024字节
* @param string $ending
* 行尾分隔符,默认为\n
* @return array
* 读取成功则返回字符串数组,文件尾的字符串靠前,读取失败则返回false
*/
function tail($filename, $rows, $size = 1024, $ending = "\n") {
$ret = false;
if ($rows > 0 && $fp = fopen($filename, 'rb')) {
$pos = filesize($filename);
$ret = array();
flock($fp, LOCK_SH);

$data = '';
$found = 0;
while ($found < $rows) {
$pos = $pos - $size;
if ($pos < 0) {
$size = 1024 + $pos;
$pos = 0;
}
fseek($fp, $pos, SEEK_SET);
$data = fread($fp, $size) . $data;
$tmp = explode($ending, $data);
$count = count($tmp);
for ($i = 1; $i < $count; $i++) {
$ret[] = $tmp[$count - $i];
$found++;
if ($found >= $rows) {
break;
}
}
$data = $tmp[0];
if ($pos <= 0) {
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
}
return $ret;
}


相关文档:

php 不用COM 生成excel文件


 
用php生成excel文件
<?
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
e ......

PHP中getenv()函数

getenv()   取得系统的环境变量(预定义变量)
$spager=getenv('SERVER_NAME');
“PHP_SELF”
当前正在执行脚本的文件名,与 document root 相关。举例来说,在 URL 地址为 [url]http://example.com/test.php/foo.bar[/url] 的脚本中使用 $_SERVER['PHP_SELF'] 将会得到 /test.php/foo.bar 这个结 ......

PHP中的MYSQL常用函数总结

PHP中的MYSQL常用函数总结
1、mysql_connect()-建立数据库连接
格式:
    resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
例:
    $conn = @mysql_connect("localhost", "username", "password") or dir( ......

Xjwebs 美国5GB豪华免费PHP主机空间 cPanel面板

今天给大家介绍的事一款来自美国的豪华主机空间,空间类型是PHP的,速度很快,主机配置也很好(4CPU)!
闲话不说了,看免费空间介绍吧:
空间大小:5GB
月流量:10GB
免费MySQL数据库: 无限制
支持脚本:CGI, PHP, FrontPage Extensions, Perl, Python.
支持FTP
支持域名绑定
有免费邮局,是cPanel面板的,支 ......

PHP安装问题:编译安装php5.2.0时出错解决方案

编译安装php5.2.0时出错解决方案
1.错误信息...................如下
checking for mcrypt support... no
checking for mhash support... no
checking whether to include mime_magic support... no
checking for MING support... no
checking for mSQL support... no
checking for MSSQL support via FreeTDS ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号