SoFunction
Updated on 2025-03-10

Example of small message book for php file operation

This article describes a small message book about php file operation. Share it for your reference. The details are as follows:

The file is as follows:

<?php 
$path = "DB/"; //Define the path$dr = opendir($path); //Open the directorywhile($filen = readdir($dr)) //Loop reading of files in the directory{ 
  if($filen != "." and $filen != "..") 
  { 
    $fs = fopen($path.$filen, "r"); 
    echo "<B>Title:</B>".fgets($fs)."&lt;BR&gt;"; 
    echo "<B>Author:</B>".fgets($fs)."&lt;BR&gt;"; 
    echo "<B>Content:</B><PRE>".fread($fs, filesize($path.$filen))."&lt;/PRE&gt;";  
    echo "&lt;HR&gt;"; 
    fclose($fs); 
  } 
} 
closedir($dr) //Close the directory?&gt; 

The file is as follows:

&lt;?php 
$path = "DB/"; 
$filename = "S".date("YmdHis").".dat"; 
$fp = fopen($path.$filename, "w"); 
fwrite($fp, $_POST["title"]."/n"); 
fwrite($fp, $_POST["author"]."/n"); 
fwrite($fp, $_POST["content"]."/n"); 
fclose($fp); 
echo "Leave a message successfully!"; 
echo "&lt;a href="" mce_href="">Return to home page</a>"; 
?&gt; 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http:///TR/html4/"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Post a new message&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;H1&gt;&lt;p align="center"&gt;Post a new message&lt;/p&gt;&lt;/H1&gt;
&lt;form name="form1" method="post" action=""&gt;
 &lt;table width="500" border="0" align="center" cellpadding="0" cellspacing="0"&gt;
  &lt;tr&gt;
   &lt;td&gt;title&lt;/td&gt;
   &lt;td&gt;&lt;input name="title" type="text"  size="50"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;author&lt;/td&gt;
   &lt;td&gt;&lt;input name="author" type="text"  size="20"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;content&lt;/td&gt;
   &lt;td&gt;&lt;textarea name="content" cols="50" rows="10" &gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
 &lt;/table&gt;
 &lt;p align="center"&gt;
  &lt;input type="submit" value="Submit"&gt;
  &lt;input type="reset" value="Reset"&gt;
&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

I hope this article will be helpful to everyone's PHP programming.