The following code shows an Example of Creating a File in PHP.


Here we write code to accept user input in a textarea. When user clicks on the submit button, writefile.php executes.

<form name="f1" method="POST" action="writefile.php">
 Create a File:<br/>
 <textarea rows="10" cols="100" name="t1">Enter content of file....</textarea>
 <input type="submit" value="Create File"/>


Here, first we fetch the data using the POST variable. After that, the fopen() function opens the file in write mode. Further, the fwrite() function writes the data. Finally, the fclose() function closes the file.

echo '<h1>Example of creating file in PHP</h1>';


Input File Data
Input File Data
Creating a File in PHP
Creating a File in PHP
File has been created
File has been created

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

PHP Practice Questions