Fread and fgetc in PHP get the contents of the file in bytes or characters, why fgetc can't read the characters?

Source: Internet
Author: User
Hello everyone, there is a question about PHP processing files, ask you.

I wrote a piece of code to experience Fread () and fgetc (), and the contents of the file Test.txt are this is a test file. TXT file encoded as Utf-8 format


  
   

The above code can get the first character T, if the second parameter of Fread is less than 4 then nothing is read!

So the following fgetc () is also blank content.


  
   

Do not get the content

My computer is a Windows system

How do you understand the second argument of Fread () and a character in fgetc?

Is the reason for the file encoding format?

How should PHP handle file encoding format?

Thank you!

Reply content:

Hello everyone, there is a question about PHP processing files, ask you.

I wrote a piece of code to experience Fread () and fgetc (), and the contents of the file Test.txt are this is a test file. TXT file encoded as Utf-8 format


  
   

The above code can get the first character T, if the second parameter of Fread is less than 4 then nothing is read!

So the following fgetc () is also blank content.


  
   

Do not get the content

My computer is a Windows system

How do you understand the second argument of Fread () and a character in fgetc?

Is the reason for the file encoding format?

How should PHP handle file encoding format?

Thank you!

I have tried both ways, and I have no problem with the test. I use the Mac. Therefore, the initial judgment should be the system file encoding relationship.
In addition, there are warnings in the official documents of Fread:
Warning:在区分二进制文件和文本文件的系统上(如 Windows)打开文件时,fopen() 函数的 mode 参数要加上 'b'。

At the same time, Fgetc's official document has such a hint:
Note: 此函数可安全用于二进制对象。

Workaround, there is a hint in the document.

  • Related Article

    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.