PHP read the picture, read the bytes more than the actual bytes of the picture, carefully read the PHP manual self-think the code is not related to the problem

Source: Internet
Author: User
PHP read the picture, read the bytes more than the actual bytes of the picture, read carefully the PHP manual self-Thinking code is no problem
My code is as follows:
 
 
$filepath = "C:\123.jpg";
$filesize =filesize ($filepath);
$fs =fopen ($filepath, "R");
$readlen =1024;//How many bytes per read
How many bytes are read $rlen =0;//
while (!feof ($FS)) {
$data =fread ($fs, $readlen);
$rlen + = $readlen;
}
Fclose ($FS);
echo ' File Size '. $filesize. '
';
echo ' read size '. $rlen. '
';
?>

PHP friends to see, My Computer system Windows 7, which should not be related to the system.
------Solution--------------------
This is exactly the system!
Window's file opening method has the difference between the text way and the binary way
In text mode, a \ r is attached to each \ n (not \ r \ n combination), forming \ r \ n
So the number of bytes is more.

You need to use binary to prevent images from being opened
$fs =fopen ($filepath, "R b");

or directly with file_get_contents.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.