注册 | 登录 | 搜索 
 ›› ›› 『 PHP技术讨论 』›› [转帖]PHP新手上路
共有 9448 人关注过本帖  
<< 上一主题    下一主题 >>
树形 打印

[转帖]PHP新手上路

帅哥哟,离线,有人找我吗?

头衔:、①玍㈠迣

等级:超级版主

威望:1

文章:19780

积分:37448

注册:2007-05-16 21:22:50

11  2007-06-20 21:32:11  

PHP新手上路(十一)

数据库链接

10. PHP最大的特色就是操作数据库的能力特别的强大,PHP提供对多种数据库的支持。

  通过PHP你可以轻松的连接到数据库,请求数据并将其显示在你的web站点中,甚至修改数据库中的数据。在这一节里我们主要以在互联网上跟PHP一起使用得最多的MySQL数据库为例,介绍一下相关的MySQL数据库的操作函数以及数据库的基本操作等方面的知识。

在MySQL数据库中,我们用来连接数据库的函数有两个,它们分别为:
integer mysql_connect(string host,string user,string password);
integer mysql_pconnect(string host,string user,string password);
mysql_connect函数和mysql_pconnect函数都是对指定主机上MySQL数据库的连接,如果该数据库位于一个不同的端口,则可以在主机名后加上冒号和端口号。函数的参数也可以缺省不填,如果不填参数,默认的主机名是“localhost”,用户名为数据库管理员,默认值为“root”,密码为空。与数据库连接成功之后,这两个函数都可以返回一个连接号,如果连接失败,则返回一个false值。让我们来看看下面几句语句:
<?
$db=mysql_connect("localhost","user","password");
mysql_select_db("mydb",$db);
?>
注释:
$db=mysql_connect("localhost","user","password"); 我们将mysql的链接参数,包括主机名、用户名和密码作为mysql_connect()的参数,同时得到返回值为$db,这样,在下面的语句中,我们就可以将变量$db作为一个连接mysql数据库的连接号来使用。
mysql_select_db("mydb",$db); 将PHP程序链接到mydb数据库中,这样程序与数据库的链接就完成了。

10.1 一个简易的数据库留言簿

  在完成数据库的链接之后,我们就可以对数据库进行一系列的操作。下面是一个简易的数据库留言簿程序(guestbook.php3):

  我假设你机子上的MySQL数据库以及管理MYSQL数据库的工具 Phpmyadmin_2. 0.5都已经安装完成,并且可以正常工作。

我们要做的第一件事情是创建一个留言数据库,假定名字为: mydb。

1、启动浏览器,打开Phpmyadmin_2. 0.5 的管理WEB界面。

2、在“Create new database”文本框内输入数据库名称mydb,然后按create按键。

  下一步,我们要在该留言数据库下创建一个数据表,假定名字为: guestbook。

创建该数据表的命令如下所示: 

CREATE TABLE guestbook (ID INT NOT NULL AUTO_INCREMENT, name CHAR(250), email CHAR(250), job CHAR(250), comments BLOB, PRIMARY KEY(ID)); 

最后,将下面的留言簿程序挎贝到你机子的可写目录下面,并保存成guestbook.php3文件。就这么简单,你已经有了自己的留言簿了。 

10.2 留言簿程序(guestbook.php3):

<?php 
/* $host : your MySQL-host, usually 'localhost' */
/* $user : your MYSQL-username */
/* $password : your MySQL-password */
/* $database : your MySQL-database */
/* $table : your MySQL-table */
/* $page_title : the title of your guestbook-pages */
/* $admin_mail : email-address of the administrator to send the new entries to */
/* $admin_name : the name of the administrator */
/* $html_mail : say yes if your mail-agent can handle HTML-mail, else say no */

$host = "localhost";
$user = "";
$password = "";
$database = "mydb";
$table = "guestbook";
$page_title = "pert guestbook";
$admin_mail = "pert@21cn.com";
$admin_name = "Webmaster";
$html_mail = "no";

?>
<HTML>
<HEAD>
<TITLE><?php echo $page_title; ?></TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" LINK="#000000">
<FONT FACE="Verdana" SIZE="-2">
<?

/* connect to the database */
mysql_pconnect("$host","$user","$password") or die("Can't connect to the SQL-server");
mysql_select_db("$database");

/* action=view : retrieve data from the database and show it to the user */
if($action == "view") {

/* function for showing the data */
function search_it($name) {

/* some vars */
global $offset,$total,$lpp,$dir;
global $table,$html_mail,$admin_name,$admin_mail;

/* select the data to get out of the database */
$query = "SELECT name, email, job, comments FROM $table";
$result = mysql_query($query);
$total= mysql_numrows($result);

print "<CENTER><FONT FACE="Verdana" SIZE="-2"><A HREF="guestbook.php3?action=add"disabled_by_dv__onMouseOver="window.status='Add your name';return true"disabled_by_dv__onMouseOut="window.status='';return true" TITLE="Add your name">加入留言</A></FONT></CENTER><br><br>";

if ($total== 0) {
print "<CENTER>此刻没人留言</CENTER><br><br>"; }

elseif ($total> 0) {

/* default */
$counter=0;
if ($dir=="") $dir="Next";
$lpp=5;
if ($offset==0) $offset=0;

if ($dir=="Next") {

if ($total > $lpp) {

$counter=$offset;
$offset+=$lpp;
$num=$offset;

if ($num > $total) {
$num=$total; } }

else {
$num=$total; } }

elseif ($dir=="Previous") {

if ($total > $lpp) {
$offset-=$lpp;

if ($offset < 0) {
$offset=0; }

$counter=$offset-$lpp;

if ($counter < 0)
$counter=0;
$num=$counter+$lpp; }

else {
$num=$total; } }

while ($counter < $num) {
$j=0;
$j=$counter + 1;

/* now really grab the data */
$i1=mysql_result($result,$counter,"name");
$i2=mysql_result($result,$counter,"email");
$i3=mysql_result($result,$counter,"job");
$i4=mysql_result($result,$counter,"comments");

$i4 = stripslashes ("$i4");

/* print it in a nice layout */
print "<CENTER>n";
print "<TABLE WIDTH=400 BORDER=0 ALIGN=CENTER VALIGN=TOP><TR><TD><FONT FACE="Verdana" SIZE="-2">n";
print "<HR>n";
print "<BR><B>Name:</B> $i1n";
print "<BR><B>email:</B><A HREF="mailto:$i2"disabled_by_dv__onMouseOver="window.status='Email $i2';return true"disabled_by_dv__onMouseOut="window.status='';return true" TITLE="Email $i2">$i2</A>n";
print "<BR><B>Job:</B> $i3n";
print "<BR><B>Comment:</B>n";
print "<BR>$i4n";
print "</FONT></TD></TR></TABLE>n";
print "</CENTER>n";
$counter++;
}
}
mysql_close();
}

/* execute the function */
search_it($name);

/* See if we need to put on the NEXT or PREVIOUS buttons */
if ($total > $lpp) {
echo("<form action="$PHP_SCRIPT" method="POST">n");

/* See if we need a PREVIOUS button */
if ($offset > $lpp) {
echo("<input type="submit" value="Previous" name=dir>n"); }

/* See if we need a NEXT button */
if ($offset < $total) {
echo("<input type="submit" value="Next" name=dir>n"); }

echo("<input type=hidden name="offset" value="$offset">n");
echo("<input type=hidden name="name" value="$name">n");
echo("</form>");
}
}

/* action=add : show a form where the user can enter data to add to the database */
elseif($action == "add") { ?>

<TABLE WIDTH="460" ALIGN="CENTER" VALIGN="TOP">
<TH COLSPAN="2"><P>请您填写留言</TH>
<FORM NAME="guestbook" ACTION="guestbook.php3?action=send" METHOD="POST">
<TR>
<TD ALIGN="RIGHT" VALIGN="TOP">
您的大名:</TD>
<TD><INPUT TYPE=text NAME=name></TD>
</TR>
<TR>
<TD ALIGN="RIGHT" VALIGN="TOP">
您的E-mail:</TD>
<TD>
<INPUT TYPE=text NAME=email></TD>
</TR>
<TR>
<TD ALIGN="RIGHT" VALIGN="TOP">
您的工作:</TD>
<TD>
<INPUT TYPE=text NAME=job></TD>
</TR>
<TR>
<TD ALIGN="RIGHT" VALIGN="TOP">
您的留言:</TD>
<TD>
<TEXTAREA NAME=comments COLS=40 ROWS=6></TEXTAREA>
<P>
<INPUT TYPE=submit VALUE=Submit> <INPUT TYPE=Reset VALUE=Reset> 
<A ALIGN="RIGHT" HREF="guestbook.php3?action=view"disabled_by_dv__onMouseOver="window.status='Read all comments first';return true"disabled_by_dv__onMouseOut="window.status='';return true" TITLE="Read all comments first"><FONT SIZE="-2">先观看所有的留言</FONT></A>
</TD>
</TR>
</FORM>
</TABLE>
</CENTER>

<?
}

/* action=send : add the data from the user into the database */
elseif($action == "send") {

/* check if a HTML-mail should be send or a plain/text mail */
if($html_mail == "yes") {
mail("$admin_name <$admin_mail>","PHP3 Guestbook Addition","<HTML><BODY><FONT FACE="Century Gothic"><TABLE BORDER="0" WIDTH="100%" CELLSPACING="4"><TR>$name ($email) schreef het volgende bericht in het gastenboek :</TR><TR><TD ALIGN="LEFT"> </TD><TD ALIGN="LEFT" NOWRAP> </TD></TR><TR><TD ALIGN="LEFT">$comments</TD><TD ALIGN="LEFT" NOWRAP> </TD></TR><TR><TD ALIGN="LEFT"> </TD><TD ALIGN="LEFT" NOWRAP> </TD></TR><TR><TD ALIGN="LEFT">您的留言:</TD><TD ALIGN="LEFT" NOWRAP>$name</TD></TR><TR><TD ALIGN="LEFT">您的大名:</TD><TD ALIGN="LEFT" NOWRAP>$email</TD></TR><TR><TD ALIGN="LEFT">您的email:</TD><TD ALIGN="LEFT" NOWRAP>$job</TD></TR><TR><TD ALIGN="LEFT">您的工作:</TD></TR></TABLE></BODY></FONT></HTML>", "From: $name <$email>nReply-To: $name <$email>nContent-type: text/htmlnX-Mailer: PHP/" . phpversion());
}


/* MySQL really hates it when you try to put things with ' or " characters into a database, so strip these...*/
$comments = addslashes ("$comments");
$query = "INSERT INTO guestbook VALUES('','$name', '$email', '$job', '$comments')";
$result = MYSQL_QUERY($query);

?>
<BR><P ALIGN = CENTER>感谢, <?php echo $name; ?>, 您的留言.
<BR><P ALIGN = CENTER><A HREF="guestbook.php3?action=view"disabled_by_dv__onMouseOver="window.status='View your comment now';return true"disabled_by_dv__onMouseOut="window.status='';return true" TITLE="View your comment now">观看留言</A><BR><BR>
<?

}

/* if there's no action given, then we must show the main page */
else {

/* get the number of entries written into the guestbook*/
$query = "SELECT name from guestbook";
$result = MYSQL_QUERY($query);
$number = MYSQL_NUMROWS($result);

if ($number == "") {
$entry = "还没有人留过言"; }

elseif ($number == "1") {
$entry = "目前留言人数1人"; }

else {
$entry = "目前留言人数 $number 人"; }

echo "<CENTER><BR>";
echo "<P>$entry<BR>";
echo "<H4><FONT FACE="Verdana" SIZE="3"><A HREF="guestbook.php3?action=add"disabled_by_dv__onMouseOver="window.status='请您留言';return true"disabled_by_dv__onMouseOut="window.status='';return true" TITLE="Add your name to our guestbook">请您留言</A></FONT></H4>";

if ($number > "") {
echo "<H4><FONT FACE="Verdana" SIZE="3"><A HREF="guestbook.php3?action=view"disabled_by_dv__onMouseOver="window.status='观看留言';return true"disabled_by_dv__onMouseOut="window.status='';return true" TITLE="View the names in our guestbook">观看留言</A></FONT></H4>"; }
echo "</P></CENTER>";
}
?>
<BR><SMALL><CENTER>版权所有:<A HREF="http://personal.668.cc/haitang/index.htm"disabled_by_dv__onMouseOver="window.status='pert';return true"disabled_by_dv__onMouseOut="window.status='';return true" TITLE="pert">无边天际</A></CENTER></SMALL>
</FONT>
</BODY>
</HTML> 

动网论坛商业版热卖中

动网插件,模板风格下载
动网论坛PHP官方站点
TOP

帅哥哟,离线,有人找我吗?

头衔:、①玍㈠迣

等级:超级版主

威望:1

文章:19780

积分:37448

注册:2007-05-16 21:22:50

12  2007-06-20 21:32:37  

PHP新手上路(十二)

使用PHP来操作Oracle数据库

11. 数据库连接

  在上一节里,我们已经介绍了PHP与MySQL数据库的一些基本操作知识,在互联网中有关PHP与MySQL的教程也最多。MySQL是免费的,这一点也许就吸引了不少人。由于其广泛应用,我就不想在这里赘述MySQL的使用方法了。Oracle被大量在企业应用中采用,因此我们就利用Oracle来进一步介绍PHP与数据库的连接。我们当然不会提及Oracle数据库的设计原理,原因是这已经超出了我们的讨论范围。

  PHP提供了两套函数与Oracle连接,分别是ORA_和OCI函数。其中ORA_函数略显陈旧。OCI函数更新据说更好一些。两者的使用语法几乎相差无几。如前所述,你的PHP安装选项应该可以支持两者的使用。
  想获得更多有关在Microsoft Windows平台上安装支持PHP3的Apache服务器的知识以及更多有关Oracle数据库的知识,请查阅以下URL:www.csoft.net/~vsbabu/articles/oraphp.html。 

11.1 连接

<? 
if ($conn=Ora_Logon("user@TNSNAME","password")) 
{
echo "<B>SUCCESS ! Connected to database<B>n";

else 
{
echo "<B>Failed :-( Could not connect to database<B>n";
}
Ora_Logoff($conn);
phpinfo();
?>
以上代码使用TNSNAME(在你的tnsnames.ora文件中指明)定义的Oracle数据库名称、用户名称和密码连接数据库。在成功连接的基础上,ora_logon函数返回一个非零的连接ID并储存在变量$conn中。

11.2 查询

假设与数据库已经连接就绪,下面我们就来实际的应用对数据库的查询。下面的代码演示了一个连接并查询的典型例子:
<?
/*
* 连接数据库并执行查询
*/
function printoraerr($in_cur)
{
// 检查Oracle是否出错
// 如果存在错误则显示
// 当指针被激活时每次请求Oracle后调用该函数
if(ora_errorcode($in_cur))
echo "Oracle code - ".ora_error($in_cur)."n";
return;
}
/** 主程序 */
if (!($conn=ora_logon("user@TNSNAME","password"))) 
{
echo "Connection to database failedn";
exit;
}
echo "Connected as connection - <b>$conn</b><br>n";
echo "Opening cursor ...<br>n";
$cursor=ora_open($conn); printoraerr($cursor);
echo "Opened cursor - <b>$cursor</b><br>n";
$qry="select user,sysdate from dual";
echo "Parsing the query <b>$qry</b> ...<br>n";
ora_parse($cursor,$qry,0); printoraerr($cursor);
echo "Query parsed <br>n";
echo "Executing cursor ...<br>n";
ora_exec($cursor); printoraerr($cursor);
echo "Executed cursor<br>n";
echo "Fetching cursor ...<br>n";
while(ora_fetch($cursor))
{
$user=ora_getcolumn($cursor,0); printoraerr($cursor);
$sysdate=ora_getcolumn($cursor,1); printoraerr($cursor);
echo " row = <B>$user, $sysdate </B><br>n";
}
echo "Fetched all records<br>n";
echo "Closing cursor ...<br>n";
ora_close($cursor);
echo "Closed cursor<br>n";
echo "Logging off from oracle... <br>n";
ora_logoff($conn);
echo "Logged off from oracle <br>n";
?>
(译者注:以上代码段缺少注释,请读者参考PHP Manual的Oracle数据库函数部分)

11.3 显示结果

以下代码演示了怎样查询数据库并将结果输出:
<?
function printoraerr($in_cur, $conn)
{
// 检查Oracle是否出错
// 如果存在错误则显示
// 当指针被激活时每次请求Oracle后调用该函数
// If it encountered an error, we exit immediately
if(ora_errorcode($in_cur)) 
{
echo "Oracle code - ".ora_error($in_cur)."<br>n";
ora_logoff($conn);
exit;
}
return;
}

function exequery($w_qry,$conn) 
{
$cursor=ora_open($conn); printoraerr($cursor,$conn);
ora_parse($cursor,$w_qry,0); printoraerr($cursor,$conn);
ora_exec($cursor); printoraerr($cursor,$conn);
$numrows=0;
$w_numcols=ora_numcols($cursor);
// 显示头部
echo "
<TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="2">
<TR>n";
for ($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
echo "t<TH VALIGN=TOP ALIGN=$align>".ora_columnname($cursor,$i)."</TH>n";
}
echo "</TR>n";
while(ora_fetch($cursor))
{
echo "<TR>n";
for ($i=0;$i<$w_numcols;$i++) 
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
if(ora_columntype($cursor,$i)=="LONG")
echo "<TD VALIGN=TOP ALIGN=$align><PRE>".
ora_getcolumn($cursor,$i)."</PRE></TD>n";
else
echo "<TD VALIGN=TOP ALIGN=$align>".ora_getcolumn($cursor,$i)."</TD>n";
printoraerr($cursor,$conn);
}
$numrows++;
echo "</TR>n";
}
if ($numrows==0)
echo "<TR><TD COLSPAN="$w_numcols"><B>Query returned no records
</B></TD></TR>n";
else 
{
echo "<TR>n";
echo "<TH COLSPAN="".($w_numcols-1)."" ALIGN=RIGHT>Count</TH>n";
echo "<TH ALIGN=RIGHT>$numrows</TH>n";
echo "</TR>n";
}
echo "</TABLE>n";
ora_close($cursor);
return;
}

// 主程序
if(!($conn=ora_logon("user@SID","password"))) 
{
echo "Error: Cannot connect to databasen";
exit;
}
$qry="SELECT
deptno "Dept"
,empno "Emp"
,empnm "Name"
,salary "Salary"
FROM
employee
ORDER BY 1,2";
exequery($qry);
ora_logoff($conn);
?>
(译者注:以上代码段缺少注释,请读者参考PHP Manual的Oracle数据库函数部分)

11.4 基于HTTP的Oracle登录

将以下代码加在PHP页面代码之前以确认Oracle登录。注意你必须正确设定$ SID。
<? 
if(!isset($PHP_AUTH_USER)) 
{
Header("WWW-authenticate: basic realm="$SID"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "<blockquote>
You are not authorized to enter the site
</blockquote> n";
exit;

else 
{
if (!($conn=ora_logon("$PHP_AUTH_USER@$SID",$PHP_AUTH_PW))) 
{
Header("WWW-authenticate: basic realm="$SID"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "<blockquote>
You are not authorised to enter the site
</blockquote> n";
exit;
}
}
?>  

动网论坛商业版热卖中

动网插件,模板风格下载
动网论坛商业版热卖中
TOP

帅哥哟,离线,有人找我吗?

头衔:、①玍㈠迣

等级:超级版主

威望:1

文章:19780

积分:37448

注册:2007-05-16 21:22:50

13  2007-06-20 21:33:50  

PHP新手上路(十三)

其他杂项

13.1 生成图像 

  PHP可以操作处理图像。如果你已经安装了GD库,你甚至可以利用PHP生成图像。
<?
Header("Content-type: image/gif");
$string=implode($argv," ");
$im = imagecreatefromgif("images/button1.gif");
$orange = ImageColorAllocate($im, 220, 210, 60);
$px = (imagesx($im)-7.5*strlen($string))/2;
ImageString($im,3,$px,9,$string,$orange);
ImageGif($im);
ImageDestroy($im);
?>

(译者注:以上代码段缺少注释,请读者参考PHP Manual的图像处理函数部分)

  这段代码在其他页面中通过以下标记<img src="button.php3?text">调用,然后以上的那段button.php3代码取得text值并在另外取得的图像文件中加上该值--在以上的代码中该图像文件是images/button1.gif--最后输出到浏览器。假如你想在表单域中使用图像按钮,但是又不希望在每次按钮上的文字改变后不得不重新生成新的图像,就可以利用这样简单的方法动态生成图像文件。

13.2 Cookies

  PHP支持基于HTTP的cookies。在需要时你可以像使用一般变量一样方便的使用cookie。Cookies是浏览器保存于客户端的一些信息片段,由此你可以知道是否一台特定PC上的任何人都访问过你的站点,浏览者者在你的站点上的踪迹等等。使用cookies的典型例子就是对浏览者偏好的甄别。Cookies由函数setcookie()设定。与输出HTTP标头的函数header()一样,setcookie()必须在任何实际内容杯输出到浏览器之前调用。以下是一个简单例子:

<?
if (empty($VisitedBefore)) 
{
// 如果没有设定cookie,为cookie赋上当前时间值
// 函数中的最后一个参数声明了该cookie保存的时间
// 在这个例子中是1年
// time()函数返回自1970年1月1日以来的以秒数计的时间
SetCookie("VisitedBefore",time(), time()+(60*60*24*365));

else 
{
// 欢迎浏览者再次光临
echo "Hello there, welcome back<BR>"; 
// 读取cookie并判断
if ( (time() - $VisitedBefore) >= "(60*60*24*7)" ) 
echo "Why did you take a week to come back. You should be here more often!? "; 

?>

13.3 常用函数

我们简单来看看一些常用的函数。

数组

array - 生成数组 
count - 数组元素个数 
sort - 数组排序,另有其他几种排序函数可供使用 
list - 列出数组元素 
each - 返回下一个key/value对 
current - 返回当前数组元素 
next,prev - 传回当前数组元素前后指针 

 

日期和时间

checkdate - 验证日期/时间格式 
date - 生成日期/时间格式 
time - 当前时间信息 
strftime - 格式化日期/时间 

目录、文件系统

chdir - 改变目录 
dir - 目录类别 
opendir, readdir, closedir - 开启、读取、关闭目录 
fopen, fclose - 开启、关闭文件 
fgets, fgetss - 逐行读取内容 
file - 将整个文件读入一个数组变量中 

正则表达式

ereg - 匹配正则表达式 
eregi - 大小写非敏感匹配正则表达式 
ereg_replace -匹配正则表达式并替换 
eregi_replace -大小写非敏感匹配正则表达式并替换 
split - 依规则切开字符串并以数组形势存储

 

字符串

AddSlashes - 加上斜杠后使用字符串 
echo - 输出一个或多个字符串 
join, implode - 将数组元素合并为字符串 
htmlentities, htmlspecialchars - 将HTML特殊字符转换为HTML标记形式 
split - 依规则切开字符串并以数组形势存储 


13.4 扩展我们的范例主页

  我们将使用以上提到的一些函数和思想为我们的范例主页添加更多的动态内容。我们可以在每个页面的顶部加上导航栏,同时使得当前页自动的不被链接显示;同时还可以添加一个用户验证表单以便上传音乐、图像等文件并自动更新页面。

导航栏

  实际上就是在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 filesn";
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<BR>n";
echo "<A HREF="$HTTP_REFERER">Go Back</A><BR>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">
<BR><BR>
<INPUT TYPE="SUBMIT" VALUE="Upload File!" NAME="sendit">
<INPUT TYPE="SUBMIT" VALUE="Cancel" NAME="cancelit"><BR>
</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><BR><BR>"; }
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");
?>

  另外,你可以在文件上传的表单中加上一个输入元素去描述该上传的文件。这个元素将被存储在文件中,然后被以上的照片图库的那段代码所读出并显示出来。 

动网论坛商业版热卖中

动网插件,模板风格下载
动网论坛专用虚拟主机
TOP

帅哥哟,离线,有人找我吗?

头衔:DVPHP水盟

等级:管理员

威望:2

文章:11571

积分:21769

注册:2002-11-01 19:51:00

14  2007-07-24 22:45:12  
学PHP是否要先掌握ASP呢?
动网论坛商业版热卖中

*是否我沉默了,你才能听见我的心声?*
*是否我停止了,你才能看到我的眼泪?*
*是否我心碎了,你才会触到我的心痛?*
*是否我消失了,你才会发觉我的存在?*

                   疯狂的石头
动网论坛商业版热卖中
TOP

帅哥哟,离线,有人找我吗?

等级:论坛游侠

文章:213

积分:432

注册:2007-07-09 11:38:00

15  2007-07-27 15:04:14  
真长啊。。。。。。
1111111


现诚招代理商,TEL:0592-5589369
QQ:539382884
动网论坛商业版热卖中
TOP

帅哥哟,离线,有人找我吗?

等级:新手上路

文章:4

积分:60

注册:2007-07-29 09:39:01

16  2007-07-29 10:55:15  
PHP要能达到开发网页的程度大概需要多长时间?我懂jsp和asp.net
111111
动网论坛PHP官方站点
TOP

帅哥哟,离线,有人找我吗?

等级:新手上路

文章:1

积分:92

注册:2005-06-07 07:59:00

17  2007-08-05 09:16:11  
不太懂.......
111111
动网论坛专用虚拟主机
TOP

帅哥哟,离线,有人找我吗?

等级:新手上路

文章:1

积分:60

注册:2007-08-06 22:26:27

18  2007-08-07 00:08:25  
好文章值得学习新手第一次来到贵坛请大家以后多关照
111111
动网·网络社区源动力
TOP

帅哥哟,离线,有人找我吗?

等级:新手上路

文章:5

积分:66

注册:2007-09-20 14:34:41

19  2007-09-20 17:47:28  

新手来报道 多谢斑竹的帖子

111111
动网·网络社区源动力
TOP

帅哥哟,离线,有人找我吗?

等级:新手上路

文章:1

积分:66

注册:2007-09-20 01:09:46

20  2007-09-22 01:39:40  
支持,好东西!
111111
动网论坛专用虚拟主机
TOP

26 / 回复数 2/3 123 GO