Small message book instance for PHP File Operations
This example describes the small message book for PHP file operations. Share it with you for your reference. The details are as follows:
The Index. php file is as follows:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
<? Php
$ Path = "DB/"; // define the path
$ Dr = opendir ($ path); // open the Directory
While ($ filen = readdir ($ dr) // read objects in the directory cyclically
{
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 Post. php file is as follows:
?
|
1
2
3
4
5
6
7
8
9
10
11 |
<? 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 "message published successfully! ";
Echo "<a href =" Index. php "mce_href =" Index. php "> back to home </a> ";
?>
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 |
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"
Http://www.w3.org/TR/html4/loose.dtd>
<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 new messages </p> </H1>
<Form name = "form1" method = "post" action = "Post. php">
<Table width = "500" border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr>
<Td> title </td>
<Td> <input name = "title" type = "text" id = "title" size = "50"> </td>
</Tr>
<Tr>
<Td> author </td>
<Td> <input name = "author" type = "text" id = "author" size = "20"> </td>
</Tr>
<Tr>
<Td> content </td>
<Td> <textarea name = "content" cols = "50" rows = "10" id = "content"> </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 help you with php programming.