The use of PHP's function file (), How to solve

Source: Internet
Author: User
The use of the PHP function file ()
$FP =fopen ("Symphony. txt", "rb+");
Fseek ($fp, 0);
while ($FP) {
Echo fgets ($FP);
";
}
echo "";

$readarray =file ("classic fashion quotes. txt");
if (! $readarray) {
echo "Failed to read the file with the filename function
";
}
else File_put_contents ("quotations. txt", $readarray);
echo "";
Fclose ($FP);
?>
Why cannot the above code be implemented to write the data in the file "classic fashion quotations. txt" in the same directory to the file "quotations. txt" (newly created)?

I don't understand. Thank you for your advice!

------Solution--------------------
$FP =fopen ("Symphony. txt", "rb+");
Fseek ($fp, 0);
while (feof ($fp)) {
Echo fgets ($FP);
";
}

Fgets () takes only a single line of content from the file. Which row to take is determined by the internal pointer of the file. Fgets each row of content, the pointer forwards a row.
The feof () is used to detect if the pointer is in the end, that is, the file.
And you use $FP, and it's not going to change, so it's going to go into a dead loop.

===========
$readarray =file ("classic fashion quotes. txt");
if (! $readarray) {
echo "Failed to read the file with the filename function
";
}
else File_put_contents ("quotations. txt", $readarray);
echo "";
Fclose ($FP);

The return value of the file () function is an array type. The type that file_put_contents () accepts is a string, so the written $readarray is coerced into a string type, which is the ' 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.