PHP File open read - website software - Phptpoint.com

PHP File open read

PHP Summer Training/Internship with 2 Live Projects in Noida Fee is only 5500/-Click here to Book Your Seat.

/ins>

Read the contents of a file in PHP

PHP comes with a couple of different ways to read the contents of file.

Read the contents using file_get_contents( ) function

The easiest way to read the contents of a disk file with the file_get_contents( ) function.

This function accepts the name and path to a disk file, and read the entire file into a string variable.
Eg i


Read the contents using file( ) function

An alternative way of reading data from a file is file( ) function, which accepts the name and path to a file and reads the entire file into an array,
with each element of the array representing one line of the file.

Here’s an example which reads a file into an array and then displays it using foreach loop.
Eg ii

Reading remote files contents

All above defined file_get_contents( ) function and file( ) function support reading data from URLs using either the HTTP or FTP protocols.

Here’s an example which reads an HTML file off the Web into an array.
Eg iii

In case of slow network links, it’s sometimes more efficient to read a remote file in “chunks” to maximize the efficiency of available network bandwidth.

=> Read the contents of the file using fread( ) function

=> Read the contents of the file using fgets( ) function

=> Read the contents of the file using fgetc( ) function

To do this use the fgets( ) function to read a specific number of bytes from a file.

Here’s an example which reads an HTML file using fgets( ) function.
Eg iv


Read the external PHP file using include( ) or require( ) function

There are 4 functions to include external PHP files.

  1. inculde( )
  2. require( )
  3. inculde_once( )
  4. require_once( )

include( ) function says, a file should be developed which you want to call,
otherwise it shows waring error.

require( ) function says, a file must be developed which you want to call,
otherwise it shows Fatal error.

include_once( ) function says, a file should be called once on a page.

require_once( ) function says, a file must be called once on a page.

Keep Updating yourself