PhpMyanmar.com logo Php Tutorial
Home
Php Tutorial
 Echo
 If

File Create
Show Myanmar Version
 show-myanmar-font
Printer Friendly Page

First a file has to exist!

In this lesson you will learn how to create a file.

PHP - Creating Confusion

In PHP, a file is created using a command that is also used to open files.

This is a little confusing.

The fopen function is used to open files.

If fopen does not find the file specified (does not exist), it will create that file.

PHP - How to Create a File

The fopen function needs two information to operate correctly.

  • First, we must supply it with the name of the file.
  • Secondly, we must tell the function what we plan on doing with that file.

(i.e. read, write, append etc).

PHP Code:
$FileName = "testFile.txt";

$FileHandle = fopen($FileName, 'w');

fclose($FileHandle);

The file "testFile.txt" should be created in the same directory where this PHP code resides.

When PHP saw that "testFile.txt" does not exists and fopen created that file.

Let's look closely at the codes.

  1. $FileName = "testFile.txt";

    Here we create the name of our file, "testFile.txt" and store it into a PHP String variable $FileName.

  2. $FileHandle = fopen($FileName, 'w')?

    This bit of code actually has two parts. First we use the function fopen and give it two arguments: (1) File name and (2) We indicate that we want to write by character "w".

    Then, the fopen function returns what is called a file handle.

    It will allow us to manipulate the file and we save the file handle into the $FileHandle variable.

  3. fclose($FileHandle);

    We close the file that was opened. fclose takes the file handle that is to be closed.

Note For more detail please see Php Manual at http://www.php.net

Processing time -- 0.2343 seconds.

26a4de41967ff89b4e140ad8706388d1