PHP reads an image and reads more bytes than the actual bytes of the image. after carefully studying the PHP manual, I think the code is okay.

Source: Internet
Author: User
PHP reads an image and reads more bytes than the actual bytes. after carefully studying the PHP manual, I think the Code is okay. my Code is as follows: & lt ;? Php & nbsp; $ filepathc: 123.jpg; & nbsp; $ filesizefilesize ($ filepath); & nbsp; $ fsfopen ($ filepath PHP reads an image in bytes more than the actual number of bytes in the image. after carefully studying the PHP manual, I think the code is okay.
My Code is as follows:
  
$ Filepath = "c: \ 123.jpg ";
$ Filesize = filesize ($ filepath );
$ Fs = fopen ($ filepath, "r ");
$ Readlen = 1024; // The number of bytes read each time.
$ Rlen = 0; // The total number of bytes read.
While (! Feof ($ fs )){
$ Data = fread ($ fs, $ readlen );
$ Rlen + = $ readlen;
}
Fclose ($ fs );
Echo 'file size'. $ filesize .'
';
Echo 'read size'. $ rlen .'
';
?>

PHP friends to show, my computer system windows 7, this should have nothing to do with the system.
------ Solution --------------------
This is precisely related to the system!
Window files are opened in the text and binary modes.
In text mode, an \ r is appended to each \ n (not a \ r \ n combination) to form \ r \ n
So the number of bytes is increased.

You need to use binary to prevent Image opening
$ Fs = fopen ($ filepath, "rb ");

Or use file_get_contents directly.

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.