There is always one more blank character for PHP to read files using fread.

Source: Internet
Author: User
The code for reading an empty character from a file using fread in PHP is as follows: PHPcode $ fileSizefilesize ($ filePath); $ handlefopen ($ filePath, & quot; rb & quot ;); while (! Feof ($ handle) {var_dump (fread ($ handle, there will always be one more blank character for PHP to read files using fread
The code is as follows:

PHP code
  $ FileSize = filesize ($ filePath); $ handle = fopen ($ filePath, "rb"); while (! Feof ($ handle) {var_dump (fread ($ handle, $ fileSize); // An empty character is output once more}



Thank you!

------ Solution --------------------
Fread ($ handle, $ fileSize)
The file contains $ fileSize bytes, and the read is successful. Therefore, the program does not detect the file. Then the second loop will be executed, and the pointer is located at the last bit, and there is no readable, so the system sets the file pointer to the bottom, so feof () returns true. There is almost no difference between the php operating file system and C. Here, we have a very detailed explanation of the feof () of C.
------ Solution --------------------
In fact, I did not mention the idea. for the solution, you can set $ fileSize + 1. or you can read all the file content without repeating fread ($ handle, filesize ($ filename.
------ Solution --------------------
Trim?
------ Solution --------------------
If the file is not large, read all the arrays and remove the last one. If the file is large, you can try another method. However, if you do not find any more files, what will happen. It is not difficult to remove it.
------ Solution --------------------
Because you are on the WINDOWS platform, files are stored in the open text file. a special byte Mark file ends at the end of the file. you can use rb to open the file and read the last special byte.

Open it with r. read it with fgets.

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.