自学PHP,通过上网查资料,查书本,写了一个本地文件上传,保存,下载的小demo;
代码有不正规处,请指正,欢迎指教:
index.html代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件管理</title>
</head>
<!------------------------------------文件上传-------------------------------------------------------->
<h2>文件上传</h2>
<hr width="30%" align="left">
<form action="upload_file.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_file.php" method="get">
<input name="filename" type="text" style="width: 17%;" value="" placeholder="请输入文件名,英文不区分大小写" />
<input type="submit" value="点击搜索" />
</form>
</body>
</html>
upload_file.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,'upload/'.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_file.php代码:
<?php
/*******************************************下载*********************************************************/
header("Content-Type:text/html;charset=utf8");
$file_name = $_GET['filename'];
$download_path = "upload/";
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);
}
/******************************end******************************************/
?>
其他,编辑工具:HBuilder,服务器:PHPstudy
其中,phpini设置:
1,post_max_size = 501M
2,max_file_uploads = 20
3,post_max_size = 30M
存在问题:
1,下载功能里,为能实现不输入文件后缀就能下载的功能(如:输入abc即可下载abc.txt文件);
2,下载功能里,文件类型必须都是英文,中文文件名下载的文件报错;