Welcome to Dream.In.Code
Getting Help is Easy!

Join 117,543 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,752 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



File Access (Opening, Reading, Writing, & Closing)

 
Reply to this topicStart new topic

> File Access (Opening, Reading, Writing, & Closing)

no2pencil
Group Icon



post 9 Nov, 2007 - 02:07 PM
Post #1


File Access (Opening, Reading, Writing, & Closing)

Before we begin, you'll need to assure that php has access to the files you intend on working with.

Opening Files:

You open a file by assigning a file pointer, also known as a handle, to the file stream. The 1st parameter is the file name, while the 2nd parameter is the method by which you will be opening the file.

An example using a local file (same directory)
CODE

<?php
$handle = fopen("info.txt", "r");
?>


An example using an absolute path (full directory)
CODE

<?php
$handle = fopen("data/info.txt", "r");
?>


In both of these examples, we opened the file with read access. The following chart shows the available options:

QUOTE

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.
'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.
'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.


After you are done using the file, make sure to close it's handle.
CODE

<?php
$handle = fopen("info.txt", "r");
fclose($handle);
?>


You'll want to make sure that the file handle was successful.

CODE

<?php
$handle = fopen("info.txt", "r");
if(!$handle) {
  die('The file failed to open.');
}
fclose($handle);
?>


Reading a file:
Reading the contents of a file is very simple.
Once you open the file handle, simply issue fread the parameters of the handle, & the size of data you wish to read. If you wish to read the entire file, gather it's size with the filesize function.

CODE

<?php
$filename = "data/info.txt";
$handle = fopen($filename, "r");
if(!$handle) die('The file failed to open.');
$contents = fread($handle, filesize($filename));
fclose($handle);
?>


Writing to a file:
When you open a file with write permission, you can add text into the file.

CODE

<?php
$somecontent = "Add this to the file\n";
$handle = fopen("info.txt", "a");
if(!$handle) die('The file failed to open.');
fwrite($handle, $somecontent);
fclose($handle);
?>


Of course, this is a very crude way of doing it, as you have no way to know that the procedure didn't fail. Be sure to add error checking as you see fit.

CODE

<?php
$filename = 'data/info.txt';
$somecontent = "Add this to the file\n";
if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    echo "Success, wrote ($somecontent) to file ($filename)";
    fclose($handle);
} else {
    echo "The file $filename is not writable";
}
?>


This post has been edited by no2pencil: 15 Nov, 2007 - 11:09 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/7/08 05:48PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month