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)."<BR>"; echo "<B>Author:</B>".fgets($fs)."<BR>"; echo "<B>Content:</B><PRE>".fread($fs, filesize($path.$filen))."</PRE>"; echo "<HR>"; fclose($fs); } } closedir($dr) //Close the directory?>
The file is as follows:
<?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 "<a href="" mce_href="">Return to home page</a>"; ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/"> <html> <head> <title>Post a new message</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <H1><p align="center">Post a new message</p></H1> <form name="form1" method="post" action=""> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td>title</td> <td><input name="title" type="text" size="50"></td> </tr> <tr> <td>author</td> <td><input name="author" type="text" size="20"></td> </tr> <tr> <td>content</td> <td><textarea name="content" cols="50" rows="10" ></textarea></td> </tr> </table> <p align="center"> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </p> </form> </body> </html>
I hope this article will be helpful to everyone's PHP programming.