code.php
3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Created by PhpStorm.
* User: zhaojie
* Date: 2018/8/28
* Time: 上午11:44
*/
$log = new Log();
$param = '';
if (isset($_GET['param'])) {
$param = $_GET['param'];
}
$log->Index($param);
/**
* 项目日志管理接口
* Class LogController
* @package Apimini\Controller\Home
*/
class Log
{
public function Index($param, $del = null)
{
$serverPath = str_replace('\\','/',realpath(dirname(__FILE__).'/'));
$path = rtrim($serverPath, 'phpapi') . $param;
// 是否需要删除文件操作
if (!empty($del) || strpos(strtolower($param), '.log')) {
$delPath = $path . '/' . $del;
$this->delFile($delPath);
}
// 判断访问的是文件还是文件夹
if(strpos($param, '.')) {
$this->ReadFile($path);
} else {
echo '目录: ' . ' <a href="?param=">party</a> / ';
$this->Banner($param);
$dir = $this->ReadPath($path, $param);
echo $dir;
}
return true;
}
// 删除文件
// private function delFile($file_path)
// {
// if(file_exists($file_path)){
// unlink($file_path);
// }
// }
/**
* 读取文件
* @param $file_path
*/
private function ReadFile($file_path)
{
if(strpos($file_path,'config.') !== false || strpos($file_path,'upload') !== false || strpos($file_path,'img') !== false){
return;
}
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i] . "<br />";
}
}
}
/**
* 面包屑
* @param $param
*/
private function Banner($param)
{
$timestamp = '&t=' . time();
$url = '?param=';
$str = '';
foreach (explode('/',$param) as $item) {
if (empty($item))
continue;
$url .= '/' . $item;
$str .= '<a href="'. $url . $timestamp .'">'. $item .'</a> / ';
}
echo $str . '<hr />';
}
/**
* 读取文件夹
* @param $path
* @param $param
* @return string
*/
private function ReadPath($path, $param)
{
$timestamp = '&t=' . time();
$fileArr = [];
if(is_dir($path)){
if($dh = @opendir($path)){
while(($file = readdir($dh)) !== false){
if($file != '.' && $file != '..'){
$fileArr[] = $file;
}
}
closedir($dh); // 关闭
}
}
// 给目录文件增加点击链接
$url = '';
foreach ($fileArr as $file) {
if(strpos($file,'config.') !== false || strpos($file,'upload') !== false || strpos($file,'img') !== false){
continue;
}
$target = '';
$del = '';
if (strpos($file, '.')) {
$target = ' target = "_blank"';
}
$url .= '<a href="?param='. $param . '/' . $file . $timestamp . '" '. $target .'>' . $file . '</a> ' . $del . ' <br />';
}
return $url;
}
}