PHP File Processing-PHP source code

Source: Internet
Author: User
Ec (2); PHP file processing when written application web pages, it is usually important to be able to store data. One of the two methods is to write data to a file or store the data in a database (such as MySQL ). Of course, the easier way (no other technology is required) is to write files, read and parse them themselves. This is what I will discuss here. So let's get started: Open the reading and writing of a file, and we start to use the open function. This only opens up archive resources for our future use of the "script" ec (2); "script"

PHP File Processing

When applying for a webpage in writing, it is usually important to be able to store data. One of the two methods is to write data to a file or store the data in a database (such as MySQL ). Of course, the easier way (no other technology is required) is to write files, read and parse them themselves. This is what I will discuss here.

Therefore, let's start:

Open the reading and writing of a file, and we start to use the open function. This only opens up archival resources for our future use.


$ Filename = 'file.txt ';
$ Handle = fopen ($ filename, 'R ');
?>



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

You can see that different modes enable the file archiving function (according to table 1 ).
The simplest way to read a file is to use fread. This requires two parameters: the file resource from the previous function (open) and a length parameter.


$ Content = fread ($ handle, filesize ($ filename ));
?>

I have to use the file size to read the end of the file. Otherwise, this function will not know when to stop reading.
Writing to a file is similar and convenient. This time, we will only use fwrite, which has two arguments: File Processing resources, this string is written (the third optional parameter is the length of the content written ).
$ Handle = fopen ($ filename, 'A ');
Fwrite ($ handle, 'Hello World! ');
?>

I have to re-process the dollar variable because the previous resources are only open for read.
Shortcuts
Well, since you know how to open, read, and write all the files, there are some shortcuts here. I tend to avoid this, but it is really useless. Note that PHP5 is required.

Instead of opening fread, you can simply use file_get_contents. The only reason is the required file, which returns the content of a string.
$ Contents = file_get_contents('file.txt ');
?>

File_put_contents('file.txt ', 'Hello World! ');
?>

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.