PHP file processing ~~Study notes_PHP tutorial

WBOY
Release: 2016-07-13 10:02:41
Original
898 people have browsed it

PHP file processing ~~Study Notes

The fopen function is used to open files in PHP.
The first parameter of this function contains the name of the file to be opened, and the second parameter specifies which mode to use to open the file;

Files may be opened in the following modes:
-------------------------------------------------- ----------------------------------------
Mode | Description
-------------------------------------------------- ----------------------------------------
r | Read only. Start at the beginning of the file.
-------------------------------------------------- ----------------------------------------
r+ | read/write. Start at the beginning of the file.
-------------------------------------------------- ----------------------------------------
w | Write only. Opens and clears the contents of the file; if the file does not exist, creates a new file.
-------------------------------------------------- ----------------------------------------
w+ | read/write. Opens and clears the contents of the file; if the file does not exist, creates a new file.
-------------------------------------------------- ----------------------------------------
a | Append. Opens and writes to the end of the file, or creates a new file if it does not exist
-------------------------------------------------- ---------------------------- ----------
a+ | read/append. Maintain file contents by writing to the end of the file.
-------------------------------------------------- ----------------------------------------
x | Write only. Create new file. Returns FALSE if the file already exists.
-------------------------------------------------- ----------------------------------------
x+ | read/write. Create new file. If the file already exists, returns FALSE and an error.
-------------------------------------------------- ----------------------------------------

Note: If fopen() cannot open the specified file, it returns 0 (false).

-------------------------------------------------- ----------------------------------------

If fopen() cannot open the specified file, the following example will generate a message:


$file = fopen("lala.txt", "r") or exit("unable to open the file");
?>
-------------------------------------------------- ----------------------------------------
Close file:

The fclose() function is used to close an open file.


$file = fopen("test.txt","r");

//some code to be executed

fclose($file);
?>
-------------------------------------------------- ----------------------------------------
Detect End-of-file
The feof() function detects whether the end of file (EOF) has been reached.
The feof() function is useful when looping over data of unknown length.

Note: In w, a and x modes, you cannot read open files!
-------------------------------------------------- ----------------------------------------
Read file line by line
The fgets() function is used to read a file line by line from a file.

Note: After calling this function, the file pointer will move to the next line.


$file = fopen("Kecily.txt", "r") or exit("unable to open the file");
while(!feof($file)) {
echo fgets($file)."
";
}
fclose($file);
?>
-------------------------------------------------- ----------------------------------------
Read file character by character
The fgetc() function is used to read from a file character by character.

Note: After calling this function, the file pointer will move to the next character.


$file = fopen("Kecily.txt", "r") or exit("unable to open the file");
while(!feof($file)) {
echo fgetc($file).;
}
echo "
";
fclose($file);

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/969601.htmlTechArticlePHP file processing~~Study Notes The fopen function is used to open files in PHP. The first parameter of this function contains the name of the file to be opened, and the second parameter specifies which mode to use to open...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!