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();
}
?>