https://dev.webpages.dk/  
 
PHP How to Create a File
How to Create a File:

This example will teach you how you can create a file. It will create the file as we open it, using 'fopen'. You can also write to the file in the same action as creating it. You will see that on a following page

To the right of here you see the different options, that you can use with the 'fopen', like Write, and Append.

 Create a File

How do you create a file using PHP?

This is a very simple example on how to create an empty textfile. .. or any flatfile. Simply put the above code into a .php file and call it from you webserver.

fopen have following options;

'r' (read only, file pointer at the beginning)
'r+' (read and write, file pointer at the beginning)
'w' (write only, file pointer at the beginning, zero length file, create it if it does not exist)
'w+' (read and write, file pointer at the beginning, zero length file,
create it if it does not exist)
'a' (write only, file pointer at the end, zero length file)
'a+' (read and write, file pointer at the end, zero length file)
'x' (write only, file pointer at the beginning, if exists, return FALSE)
'x+' (read and write, file pointer at the beginning, if exists, return FALSE)

 HTML

<HTML><BODY> 
<TABLE> 
<FORM METHOD='POST' ACTION=''> 
<TR> 
<TD>Give your file a name, <BR/>remember extension (.txt)</TD> 
<TD><INPUT TYPE='text' NAME='filename'></TD> 
</TR> 
<TR> 
<TD>Click here to create file: </TD> 
<TD><INPUT TYPE='submit' NAME='doit' VALUE='Create textfile'></TD> 
</TR></FORM> 
</TABLE> 
</BODY></HTML> 

 PHP

 As you see on the first tab, using 'w' or 'w+' will create the file is if doesn not exist.
 That is what we use here.

<?php
    if (isset($_POST['doit']))  { 
    $filename = $_POST['filename']; 
    $stien = getcwd(); 
    $file = fopen($stien . "/" . $filename, 'w'); 
    fclose();	 
    echo "Finished"; exit(); 
}
?>
Icons made by Freepik from www.flaticon.com This example shows you a way to create a file using PHP.
14:47:29