Search This Blog

Showing posts with label files. Show all posts
Showing posts with label files. Show all posts

file write in php

<?php

if(isset($_POST['name'])&&isset($_POST['age'])&&isset($_POST['fname']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  $fname=$_POST['fname'];
  if(!empty($name)&&!empty($age)&&!empty($fname))
  {
    $hand=fopen($fname.'.txt','w');
    fwrite($hand,$name." ");
    fwrite($hand,$age."\r\n");
    fclose($hand);
  }
 else
    echo 'fill all fields';
}
?>

<form action="file-write.php" method="POST">
name :<input type="text" name="name">
age :<input type="text" name="age">
file name : <input type="text" name="fname">
.txt
<input type="submit" value="submit">
</form>

output :


note :
1) enter all fields
2) the last field is file name
3) first it clears all data from that file and write you entered data into that file
4)if that file not exists then it creates and write that file

read a file in php

<?php

if(isset($_POST['filename']))
{
  if(!empty($_POST['filename']))
  {
    $file=$_POST['filename'].'.txt';
    if(file_exists($file))
    {
         $filename = fopen("$file", "r") ;
         while(!feof($filename))
               {
                 echo fgets($filename)."<br>";
               }
         fclose($filename);
    }
    else
    echo 'file not exits';
  }
 else
echo 'empty';

}
?>

<form action="file-read.php" method="POST">
<input type="text" name="filename">
.txt
<input type="submit" value="submit">
</form>

output :
1)enter the file name for read that file
2)that file must be text file

ex :        test.txt

delete a file in php

<?php

if(isset($_POST['fname']))
{
  if(!empty($_POST['fname']))
  {
       $name=$_POST['fname'].'.txt';
  if(@!unlink($name))
  {
      echo 'there is no file to delete';
  }
  else
      echo 'file successfully deleted';
     }
}
?>

<form action="file-delete.php" method="POST">
file name : <input type="text" name="fname">
.txt
<input type="submit" value="submit">
</form>

output :
 note :
1) enter the file name  for delete that text file

file append in php

<?php

if(isset($_POST['name'])&&isset($_POST['age'])&&isset($_POST['fname']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  $fname=$_POST['fname'];
  if(!empty($name)&&!empty($age)&&!empty($fname))
  {
      $file=$_POST['fname'].'.txt';
      if(file_exists("$file"))
      {
      $hand=fopen("$fname".'.txt','a');
      fwrite($hand,$name." ");
      fwrite($hand,$age."\r\n");
      fclose($hand);
      }
      else
      echo 'file not exist';
  }
  else
      echo 'all fielsd are not filled';

}

?>

<form action="file-append.php" method="POST">
name : <input type="text" name="name">
age  : <input type="text" name="age">
file name : <input type="text" name="fname">
.txt
<input type="submit" value="submit">
</form>

output :

note :
1) try with no input datat and click submit button
2) try with don't fill all text boxes and click on submit button
3) try with fill all text boxes and click on submit button
4)check  " test.txt " file

Share Links

Related Posts Plugin for WordPress, Blogger...