Monday, 13 June 2011

Lesson 10 file system using PHP.

Retweet this button on every post blogger
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>

4 comments:

  1. If you want to learn wordpress then start from here.I have create a tutorial with screen shots so that you can easy understand.
    Learn wordpress theme development from scratch

    ReplyDelete
  2. Pimcore is a leading open source .PHP Pimcore is a powerful and robust Zend Framework based PHP content management system (WCMS) for creating and managing digital content and assets.Innverse Technologies Provides best services in PHP Development services. choose our wide range of package and we create a website for you according your requirement.

    ReplyDelete
  3. This article is so goodBest Immigration

    ReplyDelete