[转帖]PHP入门-- 实例应用- 扩展范例主页

楼主
[转帖]PHP入门-- 实例应用- 扩展范例主页
我们将使用以上提到的一些函数和思想为我们的范例主页添加更多的动态内容。我们可以在每个页面的顶部加上导航栏,同时使得当前页自动的不被链接显示;同时还可以添加一个用户验证表单以便上传音乐、图像等文件并自动更新页面。还可以结合MYSQL数据库等,你自己可以逐渐学习。
导航栏
实际上就是在footer.inc文件中加上一段代码。假设你的web站点中所有后缀为.php3的文件都会出现在导航栏中,以下就是被存为include/navbar.inc的代码:
<?
/* 输出该导航栏,链接所有除当前页的站内.php3文件 */
# 读取目录
$d = dir("./");
echo "<P ALIGN=\"CENTER\"> | \n";
while($entry = $d->read())
{
// 忽略无文件情况
if ( !is_file($entry) )
continue;
/* 将文件名与扩展名分开。由于.是正则表达式特殊字符,应该用\引出 */
list($filenm, $fileext) = split("\.",$entry, 2);
// 忽略非.php3文件情况
if( $fileext != "php3" )
continue;
/* 现在我们已经把.php3文件都选出,下面搜寻文件中的第一行(标题)
类似$title="something";
并将以上标题内容分开,用作链接文字 */
$linknm = "";
$fp=fopen($entry,"r");
while($buffer=fgets($fp, 4096))
{
$buffer = trim($buffer);
// 我们已经把每个文件的标题放在文件的第一行以便搜索
// 但是当你改变变量名称时可能会带来大麻烦
if (ereg("title *= *\"", $buffer))
{
/* 我们已经取得了标题内容并可以在此基础上
进行去除空格等处理。
必须以PHP代码方式处理,比如$title = "blah blah" */
eval($buffer);
// 然后将链接文字显示为标题文字
$linknm = $title;
break;
}
}
fclose($fp);
if ( $entry == basename($PHP_SELF) )
echo "$linknm";
else
echo "<A HREF=\"$entry\">$linknm</A>";
echo " | ";
}
$d->close();
echo " </P>\n";
?>
照片收藏夹
我们将引用基于HTTP的验证、文件系统函数和文件上传功能维护放置图像文件的目录。
同时我们需要建立一个可以列出在该目录下所有照片的页面。
文件上传
<?
include("include/common.inc");
// 我们在这里再做一次用户验证
if(!isset($PHP_AUTH_USER))
{
Header("WWW-Authenticate: Basic realm=\"$MySiteName\"");
Header("HTTP/1.0 401 Unauthorized");
echo "Sorry, you are not authorized to upload files\n";
exit;
}
else
{
if ( !($PHP_AUTH_USER==$MyName && $PHP_AUTH_PW==$MyPassword ) )
{
// 如果是错误的用户名称/密码对,强制再次认证
Header("WWW-Authenticate: Basic realm=\"My Realm\"");
Header("HTTP/1.0 401 Unauthorized");
echo "ERROR : $PHP_AUTH_USER/$PHP_AUTH_PW is invalid.<P>";
exit;
}
}
if ( $cancelit )
{
// 当浏览者按下"取消"按钮则转向首页面
header ( "Location: front_2.php3" );
exit;
}
function do_upload () {
global $userfile, $userfile_size, $userfile_name, $userfile_type;
global $local_file, $error_msg;
global $HTTP_REFERER;
if ( $userfile == "none" ) {
$error_msg = "You did not specify a file for uploading.";
return;
}
if ( $userfile_size > 2000000 )
{
$error_msg = "Sorry, your file is too large.";
return;
}
// Wherever you have write permission below...
$upload_dir = "photos";
$local_file = "$upload_dir/$userfile_name";
if ( file_exists ( $local_file ) ) {
$error_msg = "Sorry, a file with that name already exists";
return;
};
// 你还可以由此检查文件名称/类型对以确定是何种文件:gif,jpg,mp3…
rename($userfile, $local_file);
echo "The file is uploaded
\n";
echo "<A HREF=\"$HTTP_REFERER\">Go Back</A>
\n";
}
$title = "Upload File";
include("include/header.inc");
if (empty($userfile) || $userfile=="none")
{
// 输出以下表单
?>
<FORM ACTION="<? echo "$PHP_SELF"; ?>" ENCTYPE="multipart/form-data" METHOD=POST>
<INPUT TYPE="HIDDEN" NAME="MAX_FILE_SIZE" VALUE="2000000">
<INPUT TYPE="FILE" NAME="userfile" SIZE="24" MAXLENGTH="80">


<INPUT TYPE="SUBMIT" VALUE="Upload File!" NAME="sendit">
<INPUT TYPE="SUBMIT" VALUE="Cancel" NAME="cancelit">
</FORM>
<I><FONT SIZE="2">(You may notice a slight delay while we upload your file.)</FONT></I>
<?
} else {
if ( $error_msg ) { echo "<B>$error_msg</B>

"; }
if ( $sendit ) {
do_upload ();
}
}
include("include/footer.inc");
?>
照片图库
<?
include("include/common.inc");
$title = "Gallery";
include("include/header.inc");
?>
<P>
Here are some of our family photos. This PHP script can really
be made better, by splitting into multiple pages.
</P>
<?
$d = dir("photos");
while($entry = $d->read())
{
if (is_file("photos/$entry"))
echo "<IMG SRC=\"photos/$entry\">\n";
}
$d->close();
?>
<?
include("include/footer.inc");
?>

电脑版 Page created in 0.0781 seconds with 4 queries.