Home » PHP, Tech Corner

File Manipulation in PHP

11 June 2009 2 Comments Posted By:Vivek

PHP provides you with a great deal of tools for creating,reading and editing files.
This section deals with the various types of manipulations possible using PHP.

For any operations to be performed,we need a file.Lets first create a file.
In PHP,a file is created using the fopen() function.It may seem odd,but i can clarify that.
When fopen() is called,it will check whether a filename(that was given as argument to fopen()) exists or not.If the file does not exist,it will create the file.
fopen() takes two arguments,the name of the file and the access mode(whether you are opening the file for reading,writing or appending)
In order to create the file,we should specify the access mode as “w” which stands for write.

createfile

Download File Creation Code

When fopen() is called,the function returns a handle,which is used for further manipulations on the file.
Now that we have created a file “test.txt”,lets do some operations on the same.

In the above code,we created test.txt using the fopen() function.Now lets use the fopen() to do some manipulations.

There are mainly three ways in which the file can be opened

The read mode,
The write mode and the
The append mode

The read mode
The read mode is specified using ‘r’ and the file is opened just for reading
the write mode
The write mode is specified using ‘w’.Here you should be careful.When ‘w’ is used,all the contents of the file are erased and the data will be written from the beginning of the file.
The append mode
The append mode is specified using ‘a’.Here the file pointer begins from the end of the file and hence we need not fear that the contents may be erased.

But,these are not the only access modes available.The file can also be opened in such a way that both reading and writing is allowed.For that we place a + sign after the file mode character.

Read/Write-’r+’
Here the file pointer is at the beginning of the file.
write/read-’w+’
It is like the r+,but it deletes all the contents of the file first.
Append-’a+’
The file pointer is at the end of the file.

fclose() should be called to free up the resources,even though the server will close all the files when the php code finishes execution.

Now,lets see how we can write to a file.To write to a file,the fwrite() function is used.First we need to open the file in write mode.fwrite() also takes two arguments,the file handle and the data to be written.So lets write some data into test.txt.

writefile

Download File Write Code

Note that if at all a test.txt file exists,the contents in the file is over written by “three2tango.com”.
If you want to add data into a file,you need to open the file in append mode instead of the write mode.

appendfile

Download append to file Code

Here the contents are not over written.Instead the data “three2tango.com” is appended to the end of file.

Now,lets see how we can read from a file.
To read information from a file,the fread() is used.
For reading the file,the file should be opened in read mode.
fread() also takes two arguments,the fle handle and an integer value.The integer tells the function how much data is to be read.So if you want to read 10 characters from the file,you will have to specify the integer as 10(1 character=1 byte)

readfile

Download File read Code

To read the entire contents,you need to know the file size.For this,specify as the second argument,the filesize.The filesize() function can be used for this purpose.
readfullfile

Download code to read entire file

In addition,for reading line by line,the fgets() function can be used.This takes the filehandle as the argument.
Syntax:fgets($fileHandle);

Finally,lets see how we can delete a file.For deleting a file,the unlink() function is used.Make sure that you close the file before unlinking.

fileunlink

Download File deletion Code

Note:Be careful when you do operations on files.You may end up editing the wrong file,accidentally deleting a file’s contents or filling garbage values.

Related posts:

  1. Creating a Simple Image Gallery using PHP
  2. Cookies and Sessions in PHP
  3. Data Encryption in PHP using crypt()
  4. Creating user defined functions and classes in PHP
  5. PHP Forms

2 Comments »

  • Rokko said:

    well.. it’s like I knew!

  • rokko said:

    mm.. really like it.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>