实现效果
PHP文件上传+下载代码
缺点:
BUG一堆,,没有做文件任何验证检测等,可以直接上传到文件夹
说明:
- 源代码来源于互联网
- 文件上传默认目录:file/userFile下
- 下载必须使用全名(要带后缀名),且上传的文件必须全英文,不可出现汉字
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件助手</title>
</head>
<body>
<h2>文件上传</h2>
<hr width="30%" align="left">
<form action="upload.php" method="post" enctype="multipart/form-data">
请选择您要上传的文件:<input type="file" name="myFile" /><br/>
<input type="submit" value="上传"/>
</form>
<br>
<h2>文件下载</h2>
<hr width="30%" align="left">
<form action="download.php" method="get">
<input name="filename" type="text" style="width: 17%;" value="" placeholder="请输入文件名,英文不区分大小写" />
<input type="submit" value="点击搜索" />
</form>
</body>
</html>
upload.php
<?php
header("Content_type:text/html;charset=utf8");
//session_start();
$imgname = $_FILES['myFile']['name'];
$tmp = $_FILES['myFile']['tmp_name'];
$error=$_FILES['myFile']['error'];
move_uploaded_file($tmp,'file/userFile/'.iconv("UTF-8", "gbk",$imgname));
if ($error==0) {
echo "上传成功!";
}else{
switch ($error){
case 1:
echo "超过了上传文件的最大值,请上传500M以下文件";
break;
case 2:
echo "上传文件过多,请一次上传20个及以下文件!";
break;
case 3:
echo "文件并未完全上传,请再次尝试!";
break;
case 4:
echo "未选择上传文件!";
break;
case 5:
echo "上传文件为0";
break;
}
}
?>
download.php
<?php
header("Content-Type:text/html;charset=utf8");
$file_name = $_GET['filename'];
$download_path = "file/userFile/";
if(!file_exists($download_path.$file_name)){
//Header("Content-type:text/html;charset=utf-8");
echo "文件不存在!</br>";
exit;
}else{
$file=fopen($download_path.$file_name,"r");
header('Content-Typr:application/octet-stream');
header("Accept-Ranges: bytes");
header("Content-Disposition:attachment;filename=".$file_name);
header('Content-length:'.filesize($download_path.$file_name));
readfile($download_path.$file_name);
}
?>
环境修改:
.ini文件设置:
- post_max_size = 501M
- max_file_uploads = 20
- post_max_size = 30M