PHP中文件的下载功能.

来源:互联网 发布:淘宝申请退货时间几天 编辑:程序博客网 时间:2024/05/17 05:04

经过了几个小时的苦苦搜索,终于把这个问题给解决了.

       php文件下载对于高手来说确实是小菜,可是对我新手来说就显得手足无措了.

 

入口文件down.html

 

<html>

<body>

 

<form action="download.php" method="GET"

enctype="multipart/form-data">

<label for="file">Filename:</label>

<input type="file" name="FileName" id="Fileid" value="<?php echo $_FILES["file"]["name"];?>" /> 

<h1></h1>

<input  type="submit" name="submit" value="download" />

</form>

 

</body>

</html>

 

 

 

 

php写的处理文件

 

download.php

 

 

<?php

if( empty($_GET['FileName'])){

    echo'<script> alert("非法连接 !"); location.replace ("./fileload.html") </script>'; exit();

}

 

$file_name=$_GET['FileName'];

 

if   (!file_exists($file_name))   {   //检查文件是否存在  

  echo   "文件找不到";  

  exit;    

  }   else   {  

$file = fopen( $file_name,"r"); // 打开文件

 

// 输入文件标签

$array=explode('//',$file_name);

 

$lenth=sizeof($array);

 

$filename=$array[$lenth-1];

 

Header("Content-type: application/cotet-stream");

 

Header("Accept-Ranges: bytes");

Header("Accept-Length: ".filesize( $file_name));

Header("Content-Disposition: attachment; filename=".$filename);/*这样就可以显示默认的下载名称为你要下载的文件的名称和类型*/

 

// 输出文件内容

echo fread($file,filesize( $file_name));

fclose($file);

exit();

}

 

?>       

 

 

 

以上代码是自己在网上找的,可是都不很齐全.记过了一段时间的调试与修改.终于把功能实现了.由于太兴奋了第一时间拿来和大家分享了.

记在这里以备后用.

原创粉丝点击