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

createfile.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>
 <br/><br/>
 <input type="submit" value="Create File"/>
</form>

writefile.php

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.

<?php
echo '<h1>Example of creating file in PHP</h1>';
$filedata=$_POST['t1'];
$filename='myfile.txt';
$file1=fopen($filename,'w');
fwrite($file1,$filedata);
fclose($file1);
?>

Output

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