写本文目的主要是基于没有服务器登陆权限,项目发布到测试站或正式站之后,无法查看代码中记录的错误信息日志,此时需要一个脚本显示服务器上记录的日志信息,准备两个方法如下:(样式可自行调节)
//判断是打开、查看、下载
function show ($dir_base, $req_path) {
if(!empty($req_path)) {
$path = $dir_base.$req_path;
if(is_dir($path)) {
header("Content-type: text/html; charset=utf-8");
list_dir($path, $dir_base);
}elseif(is_file($path)) {
if($_REQUEST['type'] == 'download') {
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($path));
readfile($path);
}elseif($_REQUEST['type'] == 'show') {
header('Content-Type: text/plain; charset=utf-8');
header("filename=".basename($path));
$ret = readfile($path);
exit();
}
}
}else {
if(!is_dir($dir_base)) {
echo "\"$dir_base\""." is not a directory!";
exit;
}
header("Content-type: text/html; charset=utf-8");
list_dir($dir_base, $dir_base);
}
}
//列出目录或文件
function list_dir ($path, $base) {
$arr = scandir($path);
foreach($arr as $val) {
$full_path = $path."/".$val;
$href= str_replace($base, '', $full_path);
if(is_dir($full_path)) {
if($val == '.') {
continue;
}
if($val == '..' && $path == $base) {
continue;
}elseif($val == '..') {
$last= dirname($path);
$href= str_replace($base, '', $last);
}
echo '<a style="color:#1a9d17;font-weight:bold;text-decoration: none;" href="'.basename(__FILE__).'?path='.$href.'">'.$val.'</a><br/>';
}elseif(is_file($full_path)) {
echo $val." ";
echo '<a style="color:#1B97CA;text-decoration: none;" target="_blank" href="'.basename(__FILE__).'?path='.$href.'&type=show">在线查看</a> ';
echo '<a style="color:#1B97CA;text-decoration: none;" href="'.basename(__FILE__).'?path='.$href.'&type=download">下载</a> ';
$filesize = filesize($full_path);
if($filesize >=1024000){
$filesize = sprintf("%0.2f", ($filesize/1024/1024)) . "MB";
}elseif($filesize >= 1024) {
$filesize = sprintf("%0.2f", ($filesize/1024))."KB";
}else {
$filesize .= "B";
}
echo "{$filesize}<br/>";
}
}
}
调用方式如下:
$req_path = $_REQUEST['path'];
show('/log',$req_path);
测试: