PHP Learn It! PHP Create File
"Best Deal"

"Bored - Do You Agree?"
t-shirtgirlspics

Bored - Studied Hard - Enjoy Yourself "TAKE A BREAK"

"Decide Now-So Many Choices"
"Amazing Deals"
Once You Know, You Newegg

Creating Files in PHP

In this section we are going to cover creating files using PHP file system function fopen()

Creating file using fopen()

Learn PHP and MySQL fast!

Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly...

>> Order your copy now for just $39.95 <<

Using the PHP file system function fopen(), we can create a new file. Let's look at this function more closely.

fopen ($filename, $mode);

The function fopen() takes in two arguments, the filename and the mode to either open or create a file.

PHP file fopen create modes

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

For the list of all modes, please see php site.

Create file

Now let see some examples for creating a new files with fopen for reading and writing.

Example 1 - create new file for writing

<?php
$fh = fopen("myfile.txt", "w");

if($fh==false)
	die("unable to create file");
?>

The code above creates a new file myfile for writing only in the current directory.

The mode "w" creates a new file for writing, places the pointer in the beginning of the file. If the file already existed, it deletes everything in the file.

What happens when fopen fails?

When you create a file, the function fopen returns a file pointer. If the attempt to create a file fails for any reason, the function returns false.

Example 2 - create file for reading and writing

<?php
$fh = fopen(" myfile.txt", "w+");
if($fh==false)
	die("unable to create file");
?>

The code above creates a new file as does in example one but it creates the file for both reading and writing in the current directory.

The mode "w+" creates a new file for reading and writing, places the pointer in the beginning of the file. If the file already existed, it deletes everything from the file.

Similarly, you can use mode 'a' and 'a+' for creating new files. However with these modes, if the file already exists, it will not truncate the file (i.e. it will not delete any content in the file) and places the pointer at the end of the file for writing.

So, in the tutorial we learned how to create a new file using the php fopen function, next we will learn how to open existing files for reading and writing using the fopen function.

Build large scalable dynamic websites using PHP yourself
Learn PHP like a pro >>

<< PHP File Intro Top PHP File Open >>