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?