小迪安全25天-php-文件管理包含,写入,删除,下载,上传,遍历,安全。
文件上传存储,还可以存储到oss云存储桶里面,
oss存储桶用js开发的
云存储桶的key,有这些才能上传到桶里面来
而这个回应osskey泄露的安全问题,别人也可以登录到桶里面查看文件
文件包含
这样写可以传参的就不安全,用户可以包含任意文件,造成漏洞
我这png内容写的给phpinfo界面。就会显示出来
用ai生成一个html前端代码
然后写一下对应文件的属性
这就是文件夹和php文件的属性值,不相同,通过这个属性值分辨是什么文件
数组
文件的显示通过数组操作,可以取想要的部分
如何循环遍历到前端来
在写个超链接,文件就可以打开了
删除功能
删除文件就可以正常使用了
文件下载
就接受的参数不一样,然后改一下文件头部为下载的文件头
文件编辑
最麻烦的
文件编译要先打开文件,而且可以正常在页面显示,就要用到html代码,在方便后续更改就要写个文本框出来
然后设置文件提交数据的表单值,代码接受之后
打开文件,把接受的表单值对文件进行覆盖,然后关闭文件
安全问题
而现在的写法没有任何过滤,存在的安全问题就很多,而且传递的参数用户可以控制就会造成任意文件读取,下载,删除
代码分享
<?php
$path=$_GET['path'] ?? './';
$action=isset($_GET['a'])?$_GET['a']:'';//a有值就接受值,没有值就为空
$filedel=isset($_GET['file'])?$_GET['file']:'';
if(is_file($filedel)){//判断是否是正常文件
$file= basename($filedel);//获取路径
$file_path=dirname($file);//获取文件名
}elseif(is_dir($filedel)){//判断是否是路径
die('无效的文件路径参数');
}
function showfile($path)
{
if(@$d=opendir($path)){//打开目录,返回句柄
while(($filename=readdir($d))!==false){//readdir读取句柄中的文件和子目录
if($filename!="." && $filename!="..") {
$filepath = "$path/$filename";//文件名字
$filetype = filetype($filepath);//文件属性//封装成数组
$list[$filetype][] = array(
'filename' => $filename,
'filepath' => $filepath,
'filesize' => round(filesize($filepath)/1024),//文件大小 kb
'filetime' => date('Y/m/d H:i:s', filemtime($filepath)));
//文件修改时间
//echo $filetype."<br>";
//echo $filepath."<br>";
//对文件和目录进行封装成数组
//数组就是类似一个大箱子,一个大箱子里面有小盒子,每一个小盒子里面有一个值,且是相对应的可以通过在大箱子里面取其中一个小盒子获得
//获取想要的值,例如下面list[filename] 这就取到filename的值,list就是大箱子,filename就是小盒子
//但是小盒子里面还可以在放好多个小盒子来去对应的值,就是嵌套数组
//例如list[filename]['xiaodi':'xiaodise];
//这是可以通过list[filename][xiaodi],获取到xiaodise的值
}
}
}
closedir($d);//关闭句柄
return $list;//返回数组
}
$list = showfile($path);//调用数组
switch($action){//接受值
case 'del'://如果值等于del就执行下面的于语句
unlink($filedel);//删除文件函数
break;
case 'down':
header('Content-type: application/octet-stream');//固定文件头部为下载的操作
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: " . filesize($file));
readfile($file);
case 'edit':
$content=file_get_contents($file);
echo '<form name="form1" method="post" action=" ">';
echo "文件名:".$file;
echo "文件内容:<br>";
echo '<textarea name="content" id="content" cols="80" rows="10">'.htmlspecialchars($content).'</textarea>';
echo '<input type="submit" name="submit" id="submit" value="提交"';
break;
}
if(isset($_POST['content'])){
$f=fopen("$path/$file","w+");
fwrite($f,$_POST['content']);
fclose($f);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件管理界面</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.action-buttons button {
margin-right: 5px;
padding: 5px 10px;
border: none;
cursor: pointer;
border-radius: 3px;
}
.action-buttons .delete {
background-color: #ff4d4d;
color: white;
}
.action-buttons .edit {
background-color: #4CAF50;
color: white;
}
.action-buttons .download {
background-color: #008CBA;
color: white;
}
</style>
</head>
<body>
<h1>文件管理</h1>
<table>
<tr>
<th>文件名</th>
<th>大小</th>
<th>修改日期</th>
<th>路径</th>
<th>打开</th>
</tr>
<tr>
<?php foreach (@$list['dir'] as $v): ?>
<td><?php echo $v['filename']?></td>
<td><?php echo $v['filesize']?>kb</td>
<td><?php echo $v['filetime']?></td>
<td ><?php echo $v['filepath']?></td>
<td><a href="?path=<?php echo $v['filepath']?>">打开</a></td>
<?php endforeach ?>
</tr>
<tr>
<?php foreach (@$list['file'] as $l): ?>
<td><?php echo $l['filename']?></td>
<td><?php echo $l['filesize']?>kb</td></td>
<td><?php echo $l['filetime']?></td>
<td><?php echo $l['filepath']?></td>
<td><a href="?a=edit&file=<?php echo $l['filepath']?>">编辑</a></td>
<td><a href="?a=down&file=<?php echo $l['filepath']?>">下载</a></td>
<td><a href="?a=del&file=<?php echo $l['filepath']?>">删除</a></td>
</tr>
<?php endforeach ?>
</table>
</body>
</html>
ping
an