Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial pHP文件下载代码,求各位大神给看看那哪里错了

pHP文件下载代码,求各位大神给看看那哪里错了

Jun 23, 2016 pm 01:40 PM

   //文件下载函数
   //$file_path  文件下载路径 
   //$file_name  文件名
   //$file_sub_dir 下载文件子路径
   function down_file($file_name,$file_sub_dir){
   //如果文件名字是中文,转码
   $file_name=iconv("utf-8", "gb-2312", $file_name);
   //绝对路径
   $file_path=$_SERVER["DOCUMENT_ROOT"].$file_sub_dir.$file_name;
   //打开文件
   if(!file_exists($file_path)){
      echo "文件不存在!";
      return;
   }
   $fp=fopen($file_path, "r");
   
   //获取文件大小
   $file_size=filesize($file_path);
   
   //返回的文件
   header("Content-type: application/octet-stream");
   //按照字节大小返回
   header("Accept-Ranges: bytes");
   //返回文件大小
   header("Accept-length: $file_size");
   //客户端弹出对话框,对应的文件名
   header("Content-Disposition: attachment; file_name=".$file_name);
   
   //向客户端回送数据
   
   $buffer=1024;
   //为了下载安全,做个文件字节读取技术器
   $file_count=0;
   //判断文件是否结束
   while (!feof($fp) && ($file_size-$file_count>0)){
        $file_data=fread($fp, $buffer);
        //读取了多少字节
        $file_count+=$buffer;
        //把部分数据传给浏览器
         echo $file_data;
   
   }
   //关闭文件
   fclose($fp);
   }
file_down("1.jpg","/test/");
?>


回复讨论(解决方案)

下面是错误

Warning: fopen(C:/AppServ/www/test/) [function.fopen]: failed to open stream: Permission denied in C:\AppServ\www\test\FileDownServer.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\FileDownServer.php:16) in C:\AppServ\www\test\FileDownServer.php on line 22

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\FileDownServer.php:16) in C:\AppServ\www\test\FileDownServer.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\FileDownServer.php:16) in C:\AppServ\www\test\FileDownServer.php on line 26

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\FileDownServer.php:16) in C:\AppServ\www\test\FileDownServer.php on line 28

Warning: feof(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 36

Warning: fread(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 37

Warning: feof(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 36

Warning: fread(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 37

Warning: feof(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 36

Warning: fopen(C:/AppServ/www/test/) [function.fopen]: failed to open stream: Permission denied in C:\AppServ\www\test\FileDownServer.php on line 16
说的很明确了!
Permission denied 中文释义:权限被拒绝

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles