• Tutorial List

PHP Create, Write, & Appending into Files

Last updated Jul 08, 2020

How to create a file in PHP?

When you open a file in w or w+ mode, if the file does not exist, it will create the file. There are several ways to create a file, read all the modes.

When you run the index.php, if the new-file.txt does not exist in your current folder, then it will create it.

<?php
	$myNewFile = fopen('new-file.txt','w');
?>

How to write a file in PHP?

To create a file we will use the PHP fwrite() function. The PHP fwrite() function is used to write an opened file.

After successfully written a file it will return the bytes written. Otherwise, it will return FALSE.


Syntax of the PHP fwrite() function

<?php
	fwrite(FILE_HANDLE, TEXT_STRING, SIZE);
  • FILE_HANDLE
    • An opened file.
  • TEXT_STRING
    • The string you want to write to the file.
  • SIZE
    • How many sizes (bytes) you want to write.
    • Suppose, If you specify 10 bytes, then you can’t write more than 10 bytes.
<?php
    $myNewFile = fopen('new-file.txt','w') or die("Oops, There is an error.");
    
	$process = fwrite($myNewFile,'Hello World, How are you?');

    if($process){
        echo "$process bytes have been written.";
    }
    else{
        echo "Could not write the file.";
    }

	// Don't forget to close the file
	fclose($myNewFile);
?>
Browser Output
25 byte/bytes have been written.

Now, open the new-file.txt which is in the same folder as your PHP file. In the file, you can see the text that we have written.


How to Limit Writing?

<?php
    $myNewFile = fopen('new-file.txt','w') or die("Oops, There is an error.");
    
	$process = fwrite($myNewFile,'Hello World, How are you?',11);

    if($process){
        echo "$process bytes have been written.";
    }
    else{
        echo "Could not write the file.";
    }

	// Don't forget to close the file
	fclose($myNewFile);
?>
Browser Output
11 byte/bytes have been written.

new-file.txt

you can see only the – Hello World.

Hello World

How to append text into a file in PHP?

If you want to append text into a file, you have to open files with the append ('a') mode.

Append means, if an existing file has already content, and you want to add some text with the existing content, this is called append.

my-file.txt

Hello World

<?php
    $myNewFile = fopen('my-file.txt','a') or die("Oops, There is an error.");
    
	$process = fwrite($myNewFile,"\nThe PHP was created by Rasmus Lerdorf in 1994.");

    if($process){
        echo "$process bytes has been added.";
    }
    else{
        echo "Could not write the file.";
    }

    // Don't forget to close the file
    fclose($myNewFile);
?>
Browser Output
47 bytes has been added.

After appending into the my-file.txt file –

Hello World
The PHP was created by Rasmus Lerdorf in 1994.

How to write or append into a file without opening or closing the file?

If you want to write or append into a file without opening or closing the file, you can do that, by the help of the file_put_contents() function.


How does it work?

This function checks, if the file does not exist which you want to write, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

It will return the bytes written after successfully written a file. Otherwise, it will return FALSE.


Write data into a non-existing file –

<?php
    $myText = "Hello, John Doe";

    $process = file_put_contents('my-new-file.txt',$myText);

    if($process){
        echo "$process bytes have been written.";
    }
    else{
        echo "Could not write the file.";
    }
?>
Browser Output
15 bytes have been written.

Appending data into an existing file

To append data, you have to set the FILE_APPEND flag in the third parameter.

<?php
    $myText = "\nHow are you?";

    $process = file_put_contents('my-new-file.txt', $myText, FILE_APPEND);

    if($process){
        echo "$process bytes has been added.";
    }
    else{
        echo "Could not write the file.";
    }
?>
Browser Output
13 bytes has been added.

In the end, the my-new-file.txt file looks

Hello, John Doe
How are you?

Ask Questions

Your email address will not be published. Required fields are marked *