Phiras\’s Blog

December 23, 06

How to read and write files in php

Filed under: WEB-APP — phiras @ 12:58 am

It’s too easy to work with files in PHP.

To open a file for reading :

$handle = fopen($filename, "r");

“r” means that you can just read from the file, to write on the file you have to put “w”, see the next table for more details :

'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.
'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.
'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 level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
'x+' Create and open for reading and writing; 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 level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.

To read the file’s content :

$contents = fread($handle, filesize($filename));

To write to the file :

fwrite($handle, $contents ) ;

and Finlay you have to close the file :

fclose($handle);

2 Comments »

  1. […] Decent Tutorial on Files – Good supplement in case you had trouble understanding the lecture. […]

    Pingback by LIS4930 (6) Web Dev with PHP » Lecture Notes: March 19 (Take II) — March 26, 08 @ 6:52 pm

  2. really good to understand standards tags of files
    thanks……

    Comment by Abhay Raj — August 20, 08 @ 9:59 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: