do_upload_new.php (用于实现防护) <?php include_once "functions.php"; if(___________)//如果不存在 session start_session($expires); if(! isset($_SESSION['username'])) { exit('您没有权限访问此页面'); } if (!isset($_POST['upload'])) { exit('请选择需要上传的文件'); } if($_POST['path'] != 'uploads' && $_POST['path'] != 'face')/*判断 路径变量*/ { exit('路径错误'); } $target_path = 'c:/uploads/' . $_POST['path'];/*设置非 web 目录保存 文件*/ $uploaded_name = $_FILES['file']['name']; /*上传文件名*/ $temp = explode(".", $uploaded_name);/*以’.’为分隔符将字符串打散 为数组*/ $uploaded_type = ______; //end 函数获取文件后缀 $uploaded_size = $_FILES['file'][____];//$_FILES 函数获取文件大小 if($uploaded_size > 1000000) { exit('文件超过 1M 字节,上传失败'); } if(_________________________________/*strtolower()处理文件后缀*/ _________________________________ _________________________________ ) { exit('文件类型错误,上传失败'); } $fname = md5( time() . $uploaded_name ) . '.' . $uploaded_type;/* 对文件名进行 md5()处理,文件重命名*/ $target_path = $target_path . '/' . ________;//文件名 while(true) { if(!file_exists($target_path)) break; else { $fname = md5( time() . $uploaded_name ) . '.' . $uploaded_type; $target_path = $target_path . '/' . $fname; } } if(!move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo '内部错误,上传失败'; } else { echo htmlspecialchars($uploaded_name) . ' 上传成功! 当前文件名 为' .$fname; } ?>
时间: 2023-08-13 09:03:20 浏览: 156
这段 PHP 代码是用于实现文件上传功能并进行一些防护措施的。其中有一些空缺需要填充,下面是我尝试补充的代码:
```php
<?php
include_once "functions.php";
if(!isset($_SESSION)) {
start_session($expires);
}
if(!isset($_SESSION['username'])) {
exit('您没有权限访问此页面');
}
if(!isset($_POST['upload'])) {
exit('请选择需要上传的文件');
}
if($_POST['path'] != 'uploads' && $_POST['path'] != 'face') {
exit('路径错误');
}
$target_path = 'c:/uploads/' . $_POST['path'];
$uploaded_name = $_FILES['file']['name'];
$temp = explode(".", $uploaded_name);
$uploaded_type = end($temp); //获取文件后缀
$uploaded_size = $_FILES['file']['size'];//获取文件大小
if($uploaded_size > 1000000) {
exit('文件超过 1M 字节,上传失败');
}
if(strtolower($uploaded_type) != 'jpg' && strtolower($uploaded_type) != 'jpeg' && strtolower($uploaded_type) != 'png') {
exit('文件类型错误,上传失败');
}
$fname = md5( time() . $uploaded_name ) . '.' . $uploaded_type;
$target_path = $target_path . '/' . $fname;
while(true) {
if(!file_exists($target_path))
break;
else {
$fname = md5( time() . $uploaded_name ) . '.' . $uploaded_type;
$target_path = $target_path . '/' . $fname;
}
}
if(!move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo '内部错误,上传失败';
} else {
echo htmlspecialchars($uploaded_name) . ' 上传成功! 当前文件名为' .$fname;
}
?>
```
请注意,这段代码仅供参考,实际使用时需要根据具体情况进行调整和完善。
阅读全文