Lesson 10 file system using PHP.

The task is to input a drive name ,folder name , file name, file contents and file extension from the user and save it into the server hard drive.we will learn some php file handling function here.
Program :
<?php
/*php  file system scipt */
if(isset($_POST['submit'])) {
   $errors=''; //an null string
   if ($_POST['folder']=="") {
   $errors.= "<li>Please give a folder name</li>";
    }
 
    if ($_POST['file]=="") {
   $errors.= "<li>Please give a file name</li>";
    }
 
    if ($_POST['text]=="") {
   $errors.= "<li>Please give a file contents</li>";
    }
 
 
   if ($errors=='') {
   $path = $_POST['drive']."/".$_POST['folder'];
   if  (!file_eixsts( $path)) {
   mkdir($path);
   }
  $file_path = $path."/".$_POST['file'].$_POST['ext'];


    if  (!file_eixsts( $file_path)) {
    $fp = fopen($path,"w");  //opening file in write mode at the given path and return a pointer to it.
    fwrite($fp,$_POST['text']); // writing file to directory
   fclose($fp);
   echo "Your file has benn saved.";
   echo "<br/>";
    }


   else
   echo "file exists already, try again";
}
else
echo "<ul>".$errors."</ul>";


?>


<html>
<head>
<title>PHP file handling</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="form"><
<center>
<table>
<tr>
<td>Drive name</td><td><select name="drive">
<option name="c:\">C:\</option>
<option name="d:\">D:\</option>
</select></td>
</tr>

<tr>
<td>Folder name</td><td><input type="text" name="folder" /></td>
</tr>


<tr>
<td>File name</td><td><input type="text" name="file" /></td>
</tr>


<tr>
<td>File contents</td><td><textarea name="text" cols="4" rows="15"></textarea></td>
</tr>


<tr>
<td>File type</td><td><select name="ext">
<option name=".txt">*.txt</option>
<option name=".html">*.html</option>
<option name=".ppt">*.ppt</option>
<option name=".doc">*.doc</option>

</select></td>
</tr>


<tr>
<td>&nbsp;</td><td><input type="submit" name="submit" value="save" /></td>
</tr>
</table>
</center>
</form>
</body>
</html>

Bookc
Admin

Hopefully the information we provide can be useful.

Press ESC to close