About PHP File Open/read/read related knowledge

Source: Internet
Author: User
Tags fread php open file

PHP file opening and reading is very important for PHP, this article will explain in detail the PHP file open and read the relevant knowledge.

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 () 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);? >

Tip: We will then learn fread () and the fclose () function.

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);? >

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 line until End-of-filewhile (!feof ($myfile)) {  echo fgets ($myfile). "<br>";} Fclose ($myfile);? >

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-filewhile (!feof ($myfile)) {  echo fgetc ($myfile);} Fclose ($myfile);? >

Note: After calling the Fgetc () function, the file pointer moves to the next character.

This article explains the PHP file open and read the relevant knowledge, more learning materials clear focus on the PHP Chinese network can be viewed.

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.