PHP Create and Write File

In this chapter, we\’ll show you how to make a file on the server and write to it.

PHP Create File – fopen ()

A file can also be made with the fopen() function. In PHP, the same function that is used to open files is also used to create new files.

If you use fopen() on a file that doesn\’t exist, it will create it if the file is opened for writing (w) or appending (a) (a).

In the next example, a new file called \”testfile.txt\” is made. The file will be made in the same folder that contains the PHP code:

Example

 

$newfile = fopen (\”testfilesystem.txt\”, \”w\”)

PHP File Permissions

If you\’re getting errors when you try to run this code, make sure you\’ve given your PHP file permission to write to the hard drive.

PHP File Write – fwrite ()

You can write to a file with the fwrite() function.

The name of the file to write to is the first parameter of fwrite(). The string to be written is the second parameter.

In the following example, two names are written to a new file called \”testfilesystem.txt\”:

Example

 

<?php
$newfile = fopen(\”testfilesystem.txt\”, \”w\”) or die(\”Unable to open file! \”);

$txt = \”coderazaa.com\”;

fwrite($newfile , $txt);

$txt = \”seotoolking.com\”;

fwrite($newfile , $txt);

fclose($newfile ); ?>

 

Take note that we wrote twice to the file \”testfilesystem.txt.\” Every time we wrote to the file, we sent the string $txt that had \”coderazaa.com\” and \”seotoolking.com\” in it. After we were done writing, we used the fclose() function to close the file.

The \”testfilesystem.txt\” file would look like this if we opened it:

Output

 

coderazaa.com
seotoolking.com

PHP Overwriting

Now that \”testfilesystem.txt\” has some information in it, we can show what happens when we open a file that already exists to write to it. All of the current information will be ERASED, and we\’ll start with a blank file.

In the example below, we open a file called \”testfilesystem.txt\” that already exists and add some new information to it:

Example

 

<?php
$newfile = fopen(\”testfilesystem.txt\”, \”w\”) or die(\”Unable to open file! \”);

$txt = \”freevideoconvertor.com\”;

fwrite($newfile , $txt);

$txt = \”moralstory.in\”;

fwrite($newfile , $txt);

fclose($newfile );?>

 

When we open the \”testfilesystem.txt\” file now, neither John nor Jane are there. Only the data we just wrote is there:

Output

 

freevideoconvertor.com

moralstory.in

PHP Append Text

With the \”a\” mode, you can add data to a file. The \”a\” mode adds text to the end of the file, while the \”w\” mode replaces the file\’s old content and deletes it.

In the example below, we open a file called \”testfilesystem.txt\” that already exists and add some text to it:

Example

 

<?php
$myfile = fopen(\”testfilesystem.txt\”, \”a\”) or die(\”Unable to open file! \”);

$var= \”Coderazaa.com\”;

fwrite($myfile, $var);

$var= \”seotoolking.com\”;

fwrite($myfile, $var);

fclose($myfile);?>

If we now open the \”testfilesystem.txt\” file, we will see that Coderazaa.com and seotoolking.com have been added to the end of the file:

Output

 

freevideoconvertor.com

smoralstory.in

Coderazaa.com

seotoolking.com

Related Posts
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top