How to read files in PHP

Source: Internet
Author: User
PHP reads and writes files, just like ASP in the use of FSO for file read and write operations. Of course, in ASP, the FSO only reads and writes to files on the server disk that runs the current program (obviously requires a physical path), but PHP can open files for reading and writing via FTP or HTTP.

one, how PHP reads Files

The PHP read file can read files from the current server or remote server. The steps are: Open the file, read the file, and close the file.

1,php How to open a file

Using the PHP function fopen () to open a file, fopen () typically uses 2 parameters to represent the path and file mode of the open file. Like what:

$FP =fopen (".. /111cn.txt ", ' W ');


where ".. /cnbruce.txt "represents the path of an open Cnbruce.txt file (relative to the path of the current executor file), and ' W ' means that the text file is opened in a write-only manner.

Appendix: Summary of file patterns for the fopen () function

R read-only-read mode, open file, start reading from file header
r+ read-write to open the file, start reading and writing from the file header
W write-write to open the file, while the contents of the file empty, the file pointer point to the beginning of the file. If the file already exists, the file is deleted and the file is created if it does not exist
w+ Open the file in a readable and writable way, emptying the contents of the file and pointing the file pointer at the beginning of the file. If the file does not exist, the file is created
A append opens the file in write-only mode, pointing the file pointer at the end of the file. If the file does not exist, the file is created
A + Chase is readable and writable to open the file and point the file pointer at the end of the file. If the file does not exist, the file is created
The b binary is used to connect to other modes. This option is recommended to achieve a greater degree of portability

Note that if the fopen () function call fails, the function returns FALSE. Otherwise, the pointer data is returned. So generally after the file is opened \ Read and write files need to detect the existence of files before.


<?php
@ $fp =fopen ("Http://www.111cn.net/a3", ' W ');
if (! $fp)
{
Echo ' file does not exist ';
Exit
}
?>


Where the @ symbol indicates that PHP will suppress all errors generated by the current function call.

2,php How to read a file

After PHP opened the file, you need to read the file, typically usingfgets () function

The function can read one row at a time from the file, reading the data continuously, to a newline character that encounters the line, or to the full text's ending symbol EOF.

The fgets () function can only read one row of data, so if you need to read all the data for a file, use a loop statement to complete it. Like what:


while (!feof ($FP))
{
$bruce =fgets ($FP);
Echo $bruce;
}


whichfeof () functionis used to detect whether the file is finished. The only argument for this function is the file pointer (that is, the $fp file).

Of course, in PHP you can also use theReadFile () functionRead the entire file at once. This function includes opening the file, reading the file and outputting it to the browser and closing the file. Like what:


<?php
$bruce =readfile ("http://www.111cn.net");
Echo $bruce;
?>


3,php How to close a file

Usefunction fclose ()You can close the file.

two, how PHP writes the data to the file

As with PHP reading files, PHP writes to files as well: Open files, write data, and close files. The way to open and close a file already shows how PHP writes data to a file.

Usefwrite () functionLike whatfwrite (file path, write content)



<?php
$bruce =fopen ("http://www.111cn.net/", "R");
if (! $bruce)
{
Echo ' file does not exist ';
Exit
}
while (!feof ($bruce))
{
$rose =fgets ($bruce);
$james =fopen ("index.htm", "a");
Fwrite ($james, $rose);
Fclose ($james);
}
Fclose ($bruce);
?>
<a href= "index.htm" > generates local files for 111cn.net content </a>



Understand the PHP read and write files, you can save the simplest data stored in the text. You can also make a story solitaire.

=============================
Other useful file functions:

file_exists ():To see if a file exists, return a Boolean value
filesize ():View file size, direct echo output
unlink ():Delete the file and note that there is no delete function in 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.