Php file read/write problems

Source: Internet
Author: User
Php file read/write problem T, T source code is like this .... @ $ Fpfopen ($ _ FILES [& quot; file & quot;] [& quot; tmp_name & quot;], & quot; r & quot;); if ($ fp) {& nbsp; while (! Feof ($ fp) {$ count ++; $ outputfgets ($ fp, 1024); $ v php file read and write problems
T, T
The source code is like this ....
@ $ Fp = fopen ($ _ FILES ["file"] ["tmp_name"], "r ");
If ($ fp ){
While (! Feof ($ fp )){
$ Count ++;
$ Output = fgets ($ fp, 1024 );
$ Var = explode (",", $ output );
......
}
The strange thing is that the first line of information is always missing,
The file content is: 71222237, computer, JAVA database programming, Tsinghua University Press, 2003, Sun Lin, 39, 7
Then read it out, $ var [0] = 7122223
Why ......

------ Solution --------------------
What is the complete var? Var_dump
------ Solution --------------------
@ $ Fp = fopen ($ _ FILES ["file"] ["tmp_name"], "r ");
If ($ fp ){
While (! Feof ($ fp )){
$ Count ++;
$ Output = fgets ($ fp, 1024 );
Echo base64_encode ($ output );
Exit;
$ Var = explode (",", $ output );
......
}

Add the red sentences to the code and paste the results after execution.
------ Solution --------------------
Discussion

Reference:

@ $ Fp = fopen ($ _ FILES ["file"] ["tmp_name"], "r ");
If ($ fp ){
While (! Feof ($ fp )){
$ Count ++;
$ Output = fgets ($ fp, 1024 );
Echo base64_encode ($ output );
Exit;
$ Var = explode (",", $ out ......

------ Solution --------------------
Discussion
Yes. my file encoding should be correct because the result is correct at the beginning of the second line...

------ Solution --------------------
Okay, let's see why.
PHP code
Echo $ output = base64_decode ("77u/NzgyMjExMjIsIOiuoeeul + response ="); $ var = explode (",", $ output); print_r ($ var );
------ Solution --------------------
You need to consider BOM.
Your data file is UTF-8 and has a BOM header.
Print_r (unpack ('H * ', $ output ));

Array
(
[1] => efbbbf3738323231...
)

Try removing BOM from the first line.
$ Var = explode (",", substr ($ output, 3 ));


------ Solution --------------------
The BOM header is not in the indicated character set. it is invalid for the character set and affects normal data.

For example, when iconv is used for encoding conversion, isn't it truncated when an invalid character is encountered?

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.