Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Open and Write to File

Open and Write to File

A simple script used to open a file and write to it.

The path to the file to open.

// File to open
$file = '/path/to/file/example.txt';

Set up the contents that you will be writing to the file.

// Content to write to the file
$contents = "random text to add to the file for example purposes.";

Check to see if the file exists, if it doesn't print an error.

// First check to see if file exists
if(file_exists($file))
	{

Set up a handler for the file and open it.

	// Open for reading and writing
	// Add contents to bottom of the file
	$handle = fopen($file, 'a+');

Write the contents to the file, print an error if we could not write to the file.

	// Attempt to write to the file
	if(!fwrite($handle, $contents))
		{
		print "Error could not write to file '" . $file . "'";
		}

At this point we have done everything that we needed to do to the file so we close it.

	// Finally close the file and wrap up the script
	fclose($handle);

Here is the else that would be called if the file wasn't found.

else
	{
	print "Error file '" . $file . "' doesn't exist.";
	}

Here is the code in full, enjoy.

<?

// File to open
$file = "path/to/file.ext";

// Content to write to the file
$contents = "random text to add to the file for example purposes.";

// First check to see if file exists
if(file_exists($file))
	{
	// Open for reading and writing
	// Add contents to bottom of the file
	$handle = fopen($file, 'a+');

	// Attempt to write to the file
	if(!fwrite($handle, $contents))
		{
		print "Error could not write to file '" . $file . "'";
		}

	// Finally close the file and wrap up the script
	fclose($handle);
	}
else
	{
	print "Error file '" . $file . "' doesn't exist.";
	}

?>

Well that is the end of the article, hope you enjoyed it.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Open and Write to File

 Jhecht  Sun Jun 3, 2007 10:31 am  
Nice job matt. I originally started to use fopen,fclose,fwrite and so on for my original project(which i still haven't fully restored. I'll get to it one of these years. For me however, i found it alot easier to use file_get_contents and file_put_contents because the other methods are good it's just that i was using my system to edit pages, and knowing where to edit a line got annoying, i just truncated the file and used file_put_contents(then i found out file_put_contents does that for you).

for those who are a bit confused by the whole fopen() and so on, just use file_get_contents() instead.
 Vagmita  Tue Feb 15, 2011 5:31 pm  
If u have any doubt then contact meeeeee......... Vagmita@gmail.com
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles