寫本文目的主要是基於沒有服務器登陸權限,項目發布到測試站或正式站之后,無法查看代碼中記錄的錯誤信息日志,此時需要一個腳本顯示服務器上記錄的日志信息,准備兩個方法如下:(樣式可自行調節)
//判斷是打開、查看、下載
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 '下載 ';
$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}
";
}
}
}
調用方式如下:
$req_path = $_REQUEST['path'];
show('/log',$req_path);
測試: