Home >Backend Development >PHP Tutorial >PHP readfile
PHP readfile is basically an inbuilt function in the PHP library used for reading a file and then writing it to an output buffer.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
readfile ( string $file_name [, boolean $path = FALSE [, resource $context ]] ) : int
The parameters being used in the syntax:
Apart from readfile() function, below are some of the other functions that can be used for performing various kinds of operations on the files.
This function is also used to read a file and it reads the complete file into an array.
Syntax:
file ( string $file_name [, int $flag = 0 [, resource $context ]] ) : array
where file_name is the path to the file to be read. Flags are the optional fields which can be chosen from the below constants:
It returns the file present in the array upon success and false upon failure.
This function can be used to open both a file and a URL.
fopen ( string $file_name , string $mode [, bool $use_include_path = FALSE [, resource $context ]] ) : resource
Mode: This parameter mentions the kind of access required to be given to the stream.’ Below are few important modes:
It returns a file pointer resource if success and false on failure.
This function is used for binary-safe file read.
Syntax:
fread ( resource $handle , int $length ) : string
where the handle is used for referencing the file pointer.
The file is read until it reaches one of the below conditions: length in bytes must have been read, EOF is reached, socket timeout occurs. fgets(), fscanf(), ftell(), fwrite(), fopen(), fsockopen() are a few other functions used for file operations in PHP.
Given below are the examples of PHP readfile:
Code:
<?php // it is writing content of file to output // buffer used in readfile() function echo readfile("file.txt"); ?>
Output:
This shows a basic example to read a file existing in the local path. Make sure that the filename being specified in the parameter of readfile() function is created and the content to be read is present inside the file. When the readfile() function is used it reads the content of the file and prints in the output.
Code:
<?php / file contents written on output // buffer by readfile() function $file = @readfile("file.txt"); if (!$file) { print "File could not be opened"; } ?>
Output:
The previous output was a simple example without any conditions. In this example, let us see how to read and display the output of a file by using certain conditions. We are using if statement to print suppose the file is not present.
Code:
<?php $file_name = "file.txt"; $fh = fopen($file_name, 'r'); $data = fread($fh, filesize($file_name)); fclose($fh); echo $data; ?>
Output:
In this example, we are combining the use of multiple file read functions. As in all the above examples, first, we are giving the file name which needs to be read from. Then the mode of operation, ‘r’ is given to them indicating it can only be read. filesize() function takes the filename and returns the size of the file along with its data and assigns it to $data variable. By using fclose() function we are closing that file. Finally, the data is printed as output.
Code:
<?php $file_name = "file.txt"; $file = fopen( $file_name , "r" ); if( $file == false ) { echo ( "Error in opening file" ); exit(); } $size = filesize( $file_name ); $filetext = fread( $file, $size); fclose( $file ); echo ( "The size of input file in bytes is : $size\n" ); echo ("Printing details of file:\n"); echo ( $filetext ); ?>
Output:
Before running the code, make sure that the file to be read file.txt is created in the local file path. In this example first, we are declaring the file name to be read and opening that with the function fopen(). Suppose the file does not exist, using if condition we are throwing an error message. Finally, we are printing the file size and the content present in the input file.
As seen from all the above examples, readfile() is one of the main functions of PHP used for reading the file name specified in this function. Apart from readfile() we have covered a few other file operations which perform similar actions such as fopen, file, fread, fgets, fgetss, ftell, etc. A combination of all of these are basically used in accessing and performing operations on the input file.
The above is the detailed content of PHP readfile. For more information, please follow other related articles on the PHP Chinese website!