站长资讯网
最全最丰富的资讯网站

php文件下载显示找不到文件怎么办

php文件下载显示找不到文件的解决办法:首先打开相应的下载代码文件;然后从浏览器中获取字符的编码;接着使用“mb_convert_encoding”函数转换编码;最后使用“file_exists”函数实现文件下载即可。

php文件下载显示找不到文件怎么办

推荐:《PHP视频教程》

php 文件下载 以及 file_exists找不到文件的解决方案

链接:<a href="upload/file/download.php?filename=雨人工作室.doc" target="_blank" >点击下载</a>

其中php:

<?php $filename = $_GET['filename']; //从浏览器获取到的字符的编码是UTF-8,我们要用这个函数转换成GBK才能才本地找到这个文件 $filename = mb_convert_encoding($filename,'GBK','UTF-8'); echo $filename ."<br>"; if( empty($filename)){     echo'<script> alert("非法连接 !"); location.replace ("index.php") </script>'; exit(); } if   (!file_exists($filename))   {   //检查文件是否存在     echo   "文件找不到";     exit; }   else   {     $file = fopen($filename,"r"); // 打开文件     // 输入文件标签     Header("Content-type: application/octet-stream");     Header("Accept-Ranges: bytes");     Header("Accept-Length: ".filesize($filename));     Header("Content-Disposition: attachment; filename=" . $filename);     // 输出文件内容     echo fread($file,filesize($filename));     fclose($file);     exit(); } ?>

总结:如果没有对浏览器传进来的文件地址进行转码(从UTF-8转成GBK),那么file_exists函数将找不到中文名字的文件。

赞(0)
分享到: 更多 (0)

网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号