PHP File Open/read/read

Source: Internet
Author: User
Tags fread php open file

PHP Open File-fopen ()
A better way to open a file is through the fopen () function. This function gives you more options than the ReadFile () function.
In the course, we will use the text file "Webdictionary.txt":
AJAX = asynchronous JavaScript and XML
CSS = cascading Style Sheets
HTML = Hyper Text Markup Language
php = PHP Hypertext Preprocessor
SQL = structured Query Language
SVG = Scalable Vector Graphics
XML = Extensible Markup Language
The first parameter of fopen (www.bjrongjinhuiyin.com) contains the file name that is opened, and the second parameter specifies the mode in which the file is opened. If the fopen () function fails to open the specified file, the following example generates a message:
Instance
<?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Echo fread ($myfile, FileSize ("Webdictionary.txt"));
Fclose ($myfile);
?>
Running an instance
The file is opened in one of the following modes:
Pattern description
R opens the file as read-only. The file pointer starts at the beginning of the file.
W Open File is write-only. Delete the contents of the file or create a new file if it does not exist. The file pointer starts at the beginning of the file.
A open file is write-only. The existing Kim data in the file will be retained. The file pointer starts at the end of the file. Creates a new file if the file does not exist.
X create a new file as write-only. Returns FALSE and an error if the file already exists.
r+ Open the file as read/write, and the file pointer starts at the beginning of the file.
w+ Open the file for read/write. Delete the contents of the file or create a new file if it does not exist. The file pointer starts at the beginning of the file.
A + opens the file for read/write. The data already in the file will be retained. The file pointer starts at the end of the file. Creates a new file if it does not exist.
x+ Create a new file for read/write. Returns FALSE and an error if the file already exists.
PHP Read Files-fread ()
The Fread () function reads the open file.
The first parameter of Fread () contains the file name of the file to be read, and the second parameter specifies the maximum number of bytes to be read.
The following PHP code reads the "webdictionary.txt" file to the end:
Fread ($myfile, FileSize ("Webdictionary.txt"));
PHP Close File-fclose ()
The fclose () function is used to close open files.
Note: It is a good programming habit to close all of the files after they are exhausted. You do not want to open a file that consumes your server resources.
Fclose () requires the name of the file to be closed (or a variable that has a file name):
<?php
$myfile = fopen ("Webdictionary.txt", "R");
Some code to be executed ....
Fclose ($myfile);
?>
PHP read single-line file-fgets ()
The fgets () function is used to read a single line from a file.
The following example outputs the first line of the "Webdictionary.txt" file:
Instance
<?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Echo fgets ($myfile);
Fclose ($myfile);
?>
Running an instance
Note: After you call the Fgets () function, the file pointer moves to the next line.
PHP Check end-of-file-feof ()
The feof () function checks to see if "End-of-file" (EOF) has been reached.
Feof () is useful for traversing data with unknown lengths.
The following example reads the "Webdictionary.txt" file row by line until End-of-file:
Instance
<?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Output single line until End-of-file
while (!feof ($myfile)) {
Echo fgets ($myfile). "<br>";
}
Fclose ($myfile);
?>
Running an instance
PHP Read single character-fgetc ()
The fgetc () function is used to read a single character from a file.
The following example reads "Webdictionary.txt" files verbatim until End-of-file:
Instance
<?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Output single character until End-of-file
while (!feof ($myfile)) {
echo fgetc ($myfile);
}
Fclose ($myfile);
?>

PHP File Open/read/read

Related Article

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.