为什么下载图片总少几个字节
$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);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
