php read the contents of the file and write data to the file

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags check content data directory document echo exit file

php tutorial read the contents of the file and write data to the file Here is to talk about his party to write data to the file, but also his party read out the contents of the document.
* /

$ fp = fopen ($ _ server ['document_root']. "/../ data / info.dat", 'r');

if (! $ fp)
{
echo "<b> error: Error opening the file, please check the directory is correct, or try again later! </ b>";
exit
}

while (! feof ($ fp))
{
$ line = fgets ($ fp);
echo $ line;
echo ';
}
fclose ($ fp);

// write the file

$ file = "data.txt";
$ content = "content title rnwww.jzread.com second line of content"; // content to be written

if (! $ fp = fopen ($ file, 'a')) // Open the file $ file, use the additional mode, the file pointer at the beginning of the file
{
echo "Open file $ file failed!";
exit
}

if (fwrite ($ fp, $ content) === false) // Write the contents of the file
{
echo "failed to write to file!";
exit
}

echo "write file successfully!";
fclose ($ fp);

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.