Page 1 of 1

File Access (Opening, Reading, Writing, & Closing) Rate Topic: -----

#1 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Posted 09 November 2007 - 02:07 PM

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)
<?php
$handle = fopen("info.txt", "r");
?> 



An example using an absolute path (full directory)
<?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.
<?php
$handle = fopen("info.txt", "r");
fclose($handle);
?>



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

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

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

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

<?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 November 2007 - 11:09 AM


Is This A Good Question/Topic? 1
  • +

Page 1 of 1