Creating file online

In this tutorial I will show you how to create a file on your web server using your browser.

In this tutorial I will show you how to create a file on your web server using your browser.
We will create a html form which will be store the file during the form processing mechanism.

Let's start it!

Step 1.
First of all we need a html form with a text field for the filename and a textarea for the file content.
This is a straightforward task. I have to following code:

<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="filecreator">
<table>
<tr><td>File name:</td><td><input name="filename" type="text" /></td></tr>
<tr><td>File content:</td><td> <textarea name="filecontent" rows="15" cols="30"></textarea></td></tr>
<tr><td><input type="submit" name="submitBtn" value="Save file" /></td></tr>
</table>
</form>
</body>
</html>


Step 2.
Now we have a basic html form. In this step we will process the submitted data. We need to capture the filename and the filecontent from the submitted form. Below you can see how to realize this.

<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="filecreator">
<table>
<tr><td>File name:</td><td><input name="filename" type="text" /></td></tr>
<tr><td>File content:</td><td> <textarea name="filecontent" rows="15" cols="30"></textarea></td></tr>
<tr><td><input type="submit" name="submitBtn" value="Save file" /></td></tr>
</table>
</form>
</body>
</html>

<?php
// Check whether the form was submitted
if (isset($_POST['submitBtn'])){
$filename = (isset($_POST['filename'])) ? $_POST['filename'] : '' ;
$filecontent = (isset($_POST['filecontent'])) ? $_POST['filecontent'] : '' ;
}
?>

Step 3.
At this point we have the filename and filecontent as string. Before storing it we need to make some validation process. The file content is not important as it can contain any character and can be empty as well. But don't forget that displaying the file content can be dangerous as if it contains some Php or JavaScript code than the browser tries to execute it. As in this tutorial we just want to write into a file it is not a problem.
However we have to check the filename variable. In this case I only check some basics with regular expression. You can extend the validation mechanism as you want.

The used validation code is the following:

<?php
if (eregi('^[a-zA-Z0-9._#-]+',$filename)){
echo
"OK";
} else {
echo
"<p>Wrong filename!</p>";
}
?>

[newpage=Page - 2] Step 4.
Ok, we have a valid filename and filecontent. We need to sore it on the web server. To make it easy reusable we will create a separate function to store the file. This function needs 2 parameters, the filename and the filecontent. Using fopen function we try to create the file. If it is not succeed than the function return false else it creates the file, writes the content into it and close it at the end. In this case the function returns true. As result the complete save function looks like this:

<?php
function saveFile($filename,$filecontent){
$file = fopen($filename,"w");
if (
$file != false){
fwrite($file,$filecontent);
fclose($file);
return
true;
} else {
return
false;
}
}
?>

Step 5.
As final step we need to check the result of our function and inform the user whether saving file was succeed or not.

The complete code is the following:

<?php
   Â
function saveFile($filename,$filecontent){
       Â
$file fopen($filename,"w");
        if (
$file != false){
           Â
fwrite($file,$filecontent);
           Â
fclose($file);
            returnÂ
true;
        } else {
            returnÂ
false;
        }
    }
?>
<html>
   <body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="filecreator">
        <table>
          <tr><td>File name:</td><td><input name="filename" type="text" /></td></tr>
          <tr><td>File content:</td><td> <textarea name="filecontent" rows="15" cols="30"></textarea></td></tr>
          <tr><td><input type="submit" name="submitBtn" value="Save file" /></td></tr>
        </table> Â
      </form>
   </body>
</html>

<?php
   Â
// Check whether the form was submitted
   Â
if (isset($_POST['submitBtn'])){
       Â
$filename    = (isset($_POST['filename']))    ? $_POST['filename']    : '' ;
       Â
$filecontent = (isset($_POST['filecontent'])) ? $_POST['filecontent'] : '' ;
       Â
        if (
eregi('^[a-zA-Z0-9._#-]+',$filename)){
            if (
saveFile($filename,$filecontent)){
                echoÂ
"The file was saved!";
            } else {
                echoÂ
"An error occured during saving the file!";
            }
        } else {
            echoÂ
"<p>Wrong filename!</p>";
        }
    }
?>