Php function file () usage, how to solve

Source: Internet
Author: User
Php function file () Usage & lt ;? Php {fp=fopen("{{& quot;, & quot; rb + & quot;); & nbsp; fseek ($ fp, 0); & nbsp; while ($ fp) {& nbsp; echo php function file () usage
$ Fp = fopen (" .txt", "rb + ");
Fseek ($ fp, 0 );
While ($ fp ){
Echo fgets ($ fp); echo"
";
}
Echo "";

$ Readarray = file ("classic style grammar .txt ");
If (! $ Readarray ){
Echo "an error occurred while reading the file using the file function.
";
}
Else file_put_contents (" .txt", $ readarray );
Echo "";
Fclose ($ fp );
?>
In the above code, why cannot I write the data in the file "classic fashion grammar .txt" under the same directory to the file "grammar .txt" (newly created?

What do I know? Thank you for your advice!

------ Solution --------------------
$ Fp = fopen (" .txt", "rb + ");
Fseek ($ fp, 0 );
While (feof ($ fp )){
Echo fgets ($ fp); echo"
";
}

Fgets () takes only one row of content in the file. The internal pointer of the file determines the row to take. Fgets retrieves the content of each row, and the pointer moves forward.
Feof () is used to check whether the pointer has reached the end of the file.
If you use $ fp, its value will not change, so it will fall into an endless loop.

==============
$ Readarray = file ("classic style grammar .txt ");
If (! $ Readarray ){
Echo "an error occurred while reading the file using the file function.
";
}
Else file_put_contents (" .txt", $ readarray );
Echo "";
Fclose ($ fp );

The return value of the file () function is an array type. File_put_contents () accepts the string type. Therefore, $ readarray is forcibly converted to the string type, that is, 'array'
Reference: http://www.php.net/manual/zh/ref.filesystem.php

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.