PHP 简单实现文件上传、保存、下载的功能

自学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,下载功能里,文件类型必须都是英文,中文文件名下载的文件报错;

 

 

  • 3
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值