Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 为什么下载图片总少几个字节

为什么下载图片总少几个字节

Jun 23, 2016 pm 01:42 PM


$file_name="Koala.jpg";

if(!file_exists($file_name)){
echo "文件不存在";
return;
}

$fp=fopen($file_name,"r");

$file_size=filesize($file_name);
echo $file_size;
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: $file_size");
header("Content-Disposition: attachment; filename=".$file_name);

$buffer=1024;
while(!feof($fp)){
$file_data = fread($fp,$buffer);

echo $file_data;
}



fclose($fp);
?>


回复讨论(解决方案)

将echo $file_size;去掉

貌似不行,我这句是把这句话后面的注销掉后,为了看字节数才后来加的,发帖子的时候忘记去掉了

错误原因: 
你用文本方式打开了二进制文件 

这个问题图片处理经常遇到 要用

$file = fopen($file_name,"rb");


在操作二进制文件时如果没有指定 "b" 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 \r\n 字符的奇怪问题。

谢谢楼上,我试了试,貌似还是和原来一样的,不过还是谢谢你

跟我前段时间做的差不多,也是多几个字节,然后下载的图片打开失败
下面是解决办法,找了很久才搜到的

//代码之前(或之后)有输出,也可能被写入下载的文件中,所以下载的时候多出几个字节
//下载文件多出几个字节的解决方法是:使用ob_start();和ob_end_clean();来清除前面的输出;
        ob_end_clean();
        //http 下载需要的响应头
        header("Content-type: application/octet-stream"); //返回的文件
        header("Accept-Ranges: bytes");   //按照字节大小返回
        header("Content-length: $file_size"); //返回文件大小
        header("Content-Disposition: attachment; filename=".$name);//这里客户端的弹出对话框,对应的文件名

究竟是多几个字节,还是少几个字节呢?
多几个字节是可以找到代码上的原因的,少几个字节应该就是图片本身的问题了

测试可以的,我只注释了 echo $file_size;

$file_name="maze.png";if(!file_exists($file_name)){echo "文件不存在";return;}$fp=fopen($file_name,"r");$file_size=filesize($file_name);//echo $file_size;header("Content-type: application/octet-stream");header("Accept-Ranges: bytes");header("Accept-Length: $file_size");header("Content-Disposition: attachment; filename=".$file_name);$buffer=1024;while(!feof($fp)){$file_data = fread($fp,$buffer);echo $file_data;}fclose($fp);
Copy after login

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 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)

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

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

Introduction to the Instagram API

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

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

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

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles