Course Banner


File handling

PHP file Intro

Like C or C++, you can also create, update and delete etc in PHP. Though file handling I don't do much while doing a project. But, sometimes, file handling is very useful while developing any project and rather than using database for small task, you can use file handling technique to accomplish the task.

While manipulating files you should take extra care because you may do a lot of damage if you do something wrong. One of the errors include editing the wrong file, filling a hard-drive with garbage data, and accidentally deleting a file's contents etc.

In this session you will learn how you can create a file, how you can write content inside the file, how you can append the content inside existing content. And how you can delete any file.

One thing you should remember is that while manipulating with file in PHP there should be write permission on the server. Or else there will be error in file creating.

PHP file create

1. Create a file dynamically in PHP

Example :

<?php
  1. $FileName = "myFile.txt";
  2. $FileHandle = fopen($FileName, 'w') or die("can't open file");
  3. fclose($FileHandle);
  4. ?>

in the above example $FileName = "myFile.txt";. here myFile.txt is the filename which will be created after running this script.

$FileHandle = fopen($FileName, 'w') or die("can't open file");

The above code actually has two parts. First we use the function fopen and give it two arguments: our file name and ask PHP that we want to write by passing the character "w". Second, the fopen function returns what is called a file handle, which will allow us to manipulate the file. We save the file handle into the $ourFileHandle variable.

When you run the code into browser, you will see the file created in the folder.

PHP file pointers

Ok so before we proceed ahead lets know more about file handlers.

Mode

Description

'r'

Open for reading only; place the file pointer at the beginning of the file.

'r+'

Open for reading and writing; place the file pointer at the beginning of the file.

'w'

Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'w+'

Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'a'

Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.

'a+'

Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.

'x'

Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of levelE_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifyingO_EXCL|O_CREATflags for the underlying open(2) system call.

'x+'

Create and open for reading and writing; otherwise it has the same behavior as 'x'.

'c'

Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).

'c+'

Open the file for reading and writing; otherwise it has the same behavior as 'c'.

2. Open the file in PHP

Example :

<?php
  1. $ourFileName = "myFile.txt";
  2. $fh = fopen($ourFileName, 'r') or die("Can't open file");
  3. fclose($fh);
  4. ?>

Here we are trying to open the file in write mode. Since, the file does not contain any data, show, it will show a blank page when opened.

4. Write content inside the file

<?php
  1. $myFile = "myFile.txt";
  2. $fh = fopen($myFile, 'w') or die("can't open file");
  3. $stringData = "Hello Worldn";
  4. fwrite($fh, $stringData);
  5. $stringData = "New Line here n";
  6. fwrite($fh, $stringData);
  7. fclose($fh);
  8. ?>

Output:

As you can see in the above figure, this time content has been written inside the file.Remember, the file must be opened first in write mode. As we have done here.

$fh = fopen($myFile, 'w') or die("can't open file");

4. Read content inside the file

<?php
  1. $myFile = "myFile.txt";
  2. $fh = fopen($myFile, 'r');
  3. $theData = fread($fh, 5);
  4. fclose($fh);
  5.       echo$theData;
  6. ?>

Output :


$fh = fopen($myFile, 'r');
$theData = fread($fh, 15);

First, we have opened the file in read mode and then used function fread() to read the file. We have put the file pointer as 15 means it will display 15 characters only.

5. Append new code in PHP

<?php
  1. $myFile = "myFile.txt";
  2. $fh = fopen($myFile, 'a') or die("can't open file");
  3. $stringData = "New Stuff 1n";
  4. fwrite($fh, $stringData);
  5. $stringData = "New Stuff 2n";
  6. fwrite($fh, $stringData);
  7. fclose($fh);
  8. ?>

New lines will be added into the existing file myFile.txt.

As you can see in the above figure, new text has been added into the existing one.

5. Delete a file in PHP

<?php
  1. $myFile = "myFile.txt";
  2. unlink($myFile);
  3. ?>

Output :

The file is deleted from the folder.