Jump To Content

LearnHub




PHP: File Handling


When working with files in PHP you must be careful, this can be very dangerous to your files and to your server if not done properly. PHP has a lot
of built-in functions to handle files, which come in handy. File handling can be useful when you need to handle a small amount of data which is not enough to create a database for. If you don't have access to databases on your server, you can use a file to do some of the work.

First we will look at all the modes of using files:
r opens the file in read-only mode
r+ read/write a existing file(starts at the beginning)
w write-only and create the file if it doesn't exist yet(erases contents of file)
w+ read/write and create the file if it doesn't exist(erases contents of file)
a write to the end of the file even if it doesn't exist
a+ read/write to the end of the file even if it doesn't exist


Open File Example:
PHP Code Example:
<?
$file
= "sample.txt"; // Sets $file to our file name
/* Next line opens our file with the mode "w" and returns a
error if it cannot open it */
$fh = fopen($file, 'w') or die("Can't open file");
fclose($fh); // Closes our file
?>


File Write Example:
PHP Code Example:
<?
$file
= "sample.txt"; // Sets $file to our file name
/* Next line opens our file with the mode "w" and returns a
error if it cannot open it */
$fh = fopen($file, 'w') or die("Can't open file");
// Next line sets $data to the data we want to put in the file
$data = "here is the data we will write to the file";
fwrite($fh, $data); // Write the value of $data into our file
fclose($fh); // Closes our file
?>

Now lets move on to reading out files. There are a few things you need to know about reading files. There are a few different way to go about doing this. We can read the whole file, just a few characters or read it one line at a time. I will show you examples of each one.

Reading 2 Characters:
PHP Code Example:
<?
$file
= "sample.txt"; // Sets $file to our file name
/* Next line opens our file with the mode "r" and returns a
error if it cannot open it */
$fh = fopen($file, 'r') or die("Can't open file");
// Next line sets $data to the first 2 characters in our file
$data = fread($fh, 2);
fclose($fh); // closes our file
echo $data; // prints the 2 characters to the screen
?>


Reading the Whole File:
PHP Code Example:
<?
$file
= "sample.txt"; // Sets $file to our file name
/* Next line opens our file with the mode "r" and returns a
error if it cannot open it */
$fh = fopen($file, 'r') or die("Can't open file");
// Next line sets $data to all the data in our file
$data = fread($fh, filesize($file));
fclose($fh); // closes our file
echo $data; // prints the 2 characters to the screen
?>

In order to be able to read one line at a time you must use the newline character(\n) at the end of each line when writing to the file. If you don't then this will now work properly.

Reading One Line:
PHP Code Example:
<?
$file
= "sample.txt"; // Sets $file to our file name
/* Next line opens our file with the mode "r" and returns a
error if it cannot open it */
$fh = fopen($file, 'r') or die("Can't open file");
// Next line sets $data to one line of the file
$data = fgets($fh);
fclose($fh); // closes our file
echo $data; // prints the one line
?>

To delete a file in PHP you use the unlink() function. You must be sure that the file your are wanting to delete is not open in your php script when you decide to delete it, so be sure to use the fclose() function to close it. Also deleting a file can be dangerous if you accidentally delete the wrong file. So be sure that you have the right filename first.

Deleting a File:
PHP Code Example:
<?
$file = "sample.txt"; // Sets $file to our file we want to delete
unlink($file); // deletes our file in $file
?>

  1. GuggiTanvi saidFri, 30 Sep 2011 15:43:18 -0000 ( Link )

    Hello,
    File handling functions in PHP are extremely useful and userfriendly.PHP includes a lot of built-in functions for handling files and directories. You can read, write, delete, and get lots of information on files through the use of these functions……………….

    for more details please check out the following link……..

    http://mindstick.com/Articles/07d7e4cf-b11c-49ea-8f4f-27c2e7f67c09/?File%20Handling%20in%20PHP

    Thanks !!!!!

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  2. GuggiTanvi saidFri, 30 Sep 2011 15:44:18 -0000 ( Link )

    Hello,
    File handling functions in PHP are extremely useful and userfriendly.PHP includes a lot of built-in functions for handling files and directories. You can read, write, delete, and get lots of information on files through the use of these functions……………….

    for more details please check out the following link……..

    http://mindstick.com/Articles/07d7e4cf-b11c-49ea-8f4f-27c2e7f67c09/?File%20Handling%20in%20PHP

    Thanks !!!!!

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

Your Comment
Textile is Enabled (View Reference)