i

PHP Tutorial

PHP File Functions Open/Read

While using a web application, you have to works with multiple files. As in a web application you need to open a file, read it, and process it also. So, file handling is required for multiple tasks in PHP. As you create a website, you will be using multiple file functions that we will explain in the next section in detail But let us know brief about file handling.

PHP has various functions for uploading, creating, reading, and editing any file. You must be very careful when you perform manipulation on files. If something went wrong, you may also lose the information in a file.

If you want to read a file and write it to output buffer, you can use readfile() function in php.

For example, if we have a file called “Weblanguage.txt” on a server having below information.

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 below script will output the file and also return the bytes read from the file.

echo readfile(“weblanguage.txt”);

?>

readfile() is a useful function when you want to open a file and read all its contents

PHP File Functions 

As we discussed in the last section, we have various file functions that we can perform in PHP. We can open and read a file, create a file, write or update a file, and upload a file to the server. So, let us discuss the file functions in detail.

1 File Open/Read

In php, to open a file you can use fopen() function and it gives you more options than readfile() function. You get to use the option to open a file in a specific mode as fopen() function accepts a parameter that specifies in which mode the file should be opened like read, write etc.

For example: fopen(“weblanguage.txt”, “r”); will open the text file in read mode.

You get below options for the modes in fopen() function.

  1. r- It opens the file in only read format.

  2. w – It opens the file for only write mode and it erases all the contents for the file and creates a new file if the file is not present.

  3. a – It allows you to append a file and preserves the data of existing file.

  4. x – It creates a new file for write mode and if the file is already present if returns FALSE and an error.

  5. r+ - It opens a file with the read and write option.

  6. w+ - It opens a file for read and write but erases all the contents for the existing file.

  7. a+ - It opens a file for read and write but the existing data in a file is preserved.

  8. x+ - It creates a new file for read and write and returns FALSE if the file is already present.

There are some other helpful functions used with an opened file as below:

  1. fread() function is used to read the contents from an open file and it accepts two parameters. First parameter specifies the name of the file and second specifies the maximum bytes to read from that file. For example: fread($filename, filesize(“weblanguage.txt”));

  2. fclose() function is used to close a file that is already opened. It is always good to close the files after your work is finished. It accepts one parameter that specifies the name of the file you need to close. For example: fclose($filename);

  3. fgets() function is used to read a single line from the opened file. For example: fgets($filename); gets one line from the specific file.

  4. feof() function checks if you have reached end of the file and is helpful to look when you are not aware of the file length. For example, you can look like: while(! feof($filename) { //code that needs to be executed}

  5. fgetc() function is used to read a single character from the opened file. For example, fgetc($filename); It is helpful when you want to read a file character by character.