PHP file Processing

Source: Internet
Author: User

PHP file Processing

When writing a Web page, it is usually important to be able to save data. The general approach is one of two ways: write data to a file, or save it in a database (such as MySQL). Of course, the easier way (no other technology is needed) is to write the files and then read and parse themselves. That's simple that's exactly what I'm talking about here.

So let's get started:

To open a file for reading and writing, we started using the open feature. This just opens up the file resources for our later use function


<?php
$filename = &apos;file.txt&apos;;
$handle = fopen ($filename, &apos;r&apos;);
?>



It is worth mentioning that I opened the file for reading. Please note ' R ' and so on? This is the model argument and allows you to use the variable $ file to read, or write, depending on your needs.

You can see different patterns that open up the function of the profile (according to table 1).
The easiest way to read a file is to use the Fread method. This requires two parameters, file resources from previous features (open) and a length parameter.


<?php
$content = Fread ($handle, FileSize ($filename));
?>

I had to use the file size to read the end of the file. Otherwise, the feature will not know when to stop reading.
Writing to a document to do similar convenience. This time, we will only use fwrite, which has two requirement arguments: File processing resources, the string writes (the third optional parameter is the length of the content write).
<?php
$handle = fopen ($filename, &apos;a&apos;);
Fwrite ($handle, &apos; Hello world!&apos;);
?>

I had to deal with the dollar variables because the previous resources were open for reading only.
Shortcut
Well, since you know how to open, read, write all the files, a long way, here are some shortcuts. I tend to avoid these, but there's really nothing wrong with it. Please note that both of these require PHP5.

Instead of opening and fread, you can simply use file_get_contents. The only reason is the required file, and it will return the contents of a string
<?php
$contents = file_get_contents (&apos;file.txt&apos;);
?>

<?php
File_put_contents (&apos;file.txt&apos;, &apos; Hello world!&apos;);
?>

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.