Ec (2); PHP reads and writes files, just like using FSO in ASP to read and write files. Of course, in ASP, FSO only reads and writes the files on the server disk that runs the current program (obviously, the physical path needs to be obtained). However, PHP can open files through FTP or HTTP for read and write. I. How does PHP read the file "script" ec (2); "script"
PHP reads and writes files, just like using FSO in ASP to read and write files. Of course, in ASP, FSO only reads and writes the files on the server disk that runs the current program (obviously, the physical path needs to be obtained). However, PHP can open files through FTP or HTTP for read and write.
I. How does PHP read files?
PHP can read files from the current server or remote server. The steps are: open the file, read the file, and close the file.
1. How to open a file in PHP
Use PHPFunction fopen ()Open a file. fopen () generally uses two parameters to indicate the path and mode of opening the file. For example: |
$ Fp = fopen ("../111cn.txt", 'w ');
|
Specifically, "../cnbruce.txt" indicates the path of the cnbruce.txt file (relative to the path of the current execution program file), and 'w' indicates opening the text file in write-only mode.
Appendix: file mode Summary of fopen () Functions
R read-only: Read mode. open the file and start reading from the file header.
R + readable and writable mode to open a file and read and write from the file header
W write only-open the file in write mode, clear the file content, and point the file pointer to the beginning of the file. If the file already exists, the existing content of the file will be deleted; if the file does not exist, the file will be created
W + readable and writable mode to open the file, clear the file content, and point the file pointer to the beginning of the file. If the file does not exist, the file is created.
A. append the file in write-only mode and point the file pointer to the end of the file. If the file does not exist, the file is created.
A + append an object to open the object in readable and writable mode and point the object pointer to the end of the object. If the file does not exist, the file is created.
Binary B is used for connection in other modes. We recommend that you use this option for greater portability.
Note: If the fopen () function fails to be called, the function returns false. Otherwise, pointer data is returned. Therefore, you need to check whether the file exists after opening the file \ before reading and writing the file.
@ $ Fp = fopen ("http://www.111cn.net/a3", 'w ');
If (! $ Fp)
{
Echo 'file does not exist ';
Exit;
}
?>
|
The @ symbol indicates that PHP will suppress all errors generated by the current function call.
2. How does PHP read files?
After opening a file in PHP, you need to read the file.Fgets () function.
This function can read a row of content from the file each time, and it constantly reads data, the value to the line break or the end symbol EOF the full text.
The fgets () function can only read one row of data. to read all the data of a file, use a circular statement. For example:
While (! Feof ($ fp ))
{
$ Bruce = fgets ($ fp );
Echo $ bruce;
}
|
WhereFeof () functionIs used to check whether the file ends. The unique parameter of this function is the file pointer (that is, the file opened by $ fp ).
Of course, it can also be used in PHPReadfile () functionRead the entire file at a time. This function includes opening a file, reading a file, outputting it to a browser, and closing the file. For example:
$ Bruce = readfile ("http://www.111cn.net ");
Echo $ bruce;
?>
|
3. How does PHP close files?
UseFunction fclose ()You can close the file.
2. How to Write Data to files in PHP
Like PHP reading files, PHP writing files also requires opening files, writing data, and closing files. The above describes how to open and close a file. What is the data written to a file in PHP.
UseFwrite () functionFor exampleFwrite (file path, write content):
$ 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", "");
Fwrite ($ james, $ rose );
Fclose ($ james );
}
Fclose ($ bruce );
?>
A local file is generated for the content of 111cn.net.
|
After learning about PHP read/write files, you can save the simplest data into text. You can also make a story to answer the dragon.
==================================
Other useful file functions:
File_exists ():Check whether the file exists and return a Boolean Value Filesize ():View the file size and echo the output directly. Unlink ():Delete files. Note that PHP does not have the delete function. |