PHP File Handling

PHP file Handling tutorial will explain you What is File Handling in PHP? with example.

PHP gives you an awesome bargain of tools for opening, creating, uploading, and editing files. At the time you are controlling files you must be extremely watchful on the grounds that you can do a considerable measure of harm if something happens.

Regular errors include, editing the wrong file, filling a hard-drive with garbage data, and incidentally erasing a file’s content.

Normal errors include, editing the wrong file, filling a hard-drive with garbage data, and accidentally deleting a file’s content.

Opening a file with PHP fopen() function.

PHP fopen() function is used for opening file in PHP. PHP fopen() function takes two parameters,

  1. the file to be opened i.e. file name (should provide a correct path) and
  2. the mode in which the file should be opened.

The function returns a file pointer if successful, otherwise zero (false). Files are opened with PHP fopen() function for reading or writing.

Example on PHP fopen() function:

If PHP fopen() function is unable to open the file, it returns 0. PHP fopen() function can be used to exit the function with an appropriate message.

File modes for PHP fopen() function

Mode Description
r Read Only mode, with the file pointer at the start of the file.
r+ Read/Write mode, with the file pointer at the start of the file.
w Write Only mode. Truncates the file (effectively overwriting it). If the file doesn’t exist, fopen will attempt to create the file.
w+ Read/Write mode. Truncates the file (effectively overwriting it). If the file doesn’t exist, fopen will attempt to create the file.
a Append mode, with the file pointer at the end of the file. If the file doesn’t exist, fopen will attempt to create the file.
a+ Read/Append, with the file pointer at the end of the file. If the file doesn’t exist, fopen will attempt to create the file.
x Create and open for writing only. If the file already exists, fopen() will fail by returning FALSE and generating an error. If the file does not exist, attempt to create it.
x+ Create and open for reading and writing. If the file already exists, fopen() will fail by returning FALSE and generating an error. If the file does not exist, attempt to create it.

Closing a file using PHP fclose() function

When you have finished using a file, you need to close it.

You should do this with the PHP fopen() function as follows:

PHP fclose() function will return true if the file was successfully closed, or false if it wasn’t.

Reading from file using PHP fread() function

PHP fread() function is the staple for getting data out of a file.

The function requires a file handle, which we have, and an integer to tell the function how much data, in bytes, it is supposed to read.

One character is equal to one byte. If you wanted to read the first five characters then you would use five as the integer.

Example on PHP fread() function:

Output will be:

First five characters from the testFile.txt file are now stored inside $theData. You could echo this string, $theData, or write it to another file.

Check End of File

The feof() function is used to determine if the end of file is true.

PHP feof() function can be used in a while loop, to read from the file until the end of file is encountered.

Reading a file line by line

A line at a time can be read with the PHP fgets() function. Here is the sample code for that:

Reading a file character by character

You can read in a single character at a time from a file using PHP fgetc function.

Example:

SHARE
Previous articlePHP Include File
Next articlePHP File Upload
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!