学习PHP建站很久啦,很想实现PHP网站上传功能。
首先打开Dreamweaver CS5,输入以下代码:
<?
if($_COOKIE[name])
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>上传照片</title>
<link href="mystyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">相册管理系统</h1>
<p align="center"> <strong>[<?=$_COOKIE[name]?>]</strong>的相册</p>
<p align="center">
<a href="ch17-7.php">创建相册</a>
<a href="ch17-5.php">进入相册</a>
<a href="ch17-6.php">设置首页照片</a>
<a href="ch17-9.php">删除照片</a>
<a href="ch17-10.php">更改主人密码</a>
<a href="ch17-11.php">消息接收
<?
require "ch17-1.php";
$link=mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
$sql="select * from $table_ms where receiver='$_COOKIE[name]' && isread='no'";
$result=mysql_query($sql,$link) or die(mysql_error());
$num=@mysql_num_rows($result);//获得记录数
if($num!=0)
{
echo "<b>(".$num.")</b>";
}
?>
</a>
<a href="ch17-12.php">退出登录</a></p>
<?
if($_FILES[upfile][name]!="") //判断是否上传了文件
{
$album=$_POST[album]; //获取相册名
$details=nl2br($_POST[details]); //获取相片描述
$time=$_POST[time]; //获取相片上传时间
$filepath="upload/";//定义相片保存路径
$filename=$filepath.$_FILES[upfile][name];//新的路径及文件名
$size=$_FILES[upfile][size];
if(copy($_FILES[upfile][tmp_name],$filename))//复制文件的目标路径
{
unlink($_FILES[upfile][tmp_name]);//删除原有文件
require "ch17-1.php";
$link=mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
mysql_select_db($db_name,$link);
$sql="insert into $table_ps(album_name,user,img_name,size,time,details) values('$album','$_COOKIE[name]','$filename','$size','$time','$details')"; //添加新相片
if(mysql_query($sql,$link))
{
echo "<head>";
echo "<title>指定文件已经成功上传!!1秒后自动跳转查看相册</title>";
echo "<meta http-equiv=\"refresh\" content=\"1; url=ch17-5.php\">";
echo "</head>";
echo "<body><center>指定文件已经成功上传!!1秒后自动跳转查看相册......</center></body>";
echo "</html>";
}
else
{
echo "<head>";
echo "<meta http-equiv=\"refresh\" content=\"1; url=ch17-8.php\">";
echo "</head>";
echo "<body><center>文件上传失败,1秒后重新上传...</center></body>";
echo "</html>";
}
}
}
?>
<h1 align="center">上传照片</h1>
<form action="ch17-8.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="583" border="1" align="center">
<tr>
<td width="115">选择图片:</td>
<td width="424">
<input name="upfile" type="file" id="upfile" />
</td>
</tr>
<tr>
<td>选择相册</td>
<td>
<select name="album">
<?
require "ch17-1.php";
$link=mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
mysql_select_db($db_name,$link);
$sql="select * from $table_album where user='$_COOKIE[name]'";
$result=mysql_query($sql,$link);
while($row=mysql_fetch_array($result))
{
echo "<option>".$row[album_name]."</option>";
}
?>
</select></td>
</tr>
<tr>
<td>输入说明:</td>
<td>
<textarea name="details" id="details" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="Submit" value="确认提交" />
<input type="reset" name="Submit2" value="重新选择" />
</td>
</tr>
</table>
<label></label>
<p>
<label></label>
<label></label>
<input type="hidden" name="time" value="<? echo date(y年m月d日H时i分);?>" />
<p> </p>
</form>
</body>
</html>
<?
}
else
{
echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>";
echo "<meta http-equiv='refresh' content='1; url=ch17-3.php'>";
echo "</head>";
echo "<body>您不是管理员,不能进入该页,1秒后返回首页……</body>";
echo "</html>";
}
?>
2. 保存文件并调试运行
以文件名David.php保存页面,文件自动保存到站点中。进行该网页的运行与调试,运行前确保是以注册用户的身份登录成功,如图;
依次填写好各内容,选择好欲上传的照片,点击“确认提交”,若上传照片成功,则显示信息“指定文件已经成功上传!!1秒后自动跳转查看相册......
本例主要实现照片上传的功能,上传成功,则自动跳转至David.php进行查看。
要特别注意文件上传部分。
来源:oschina
链接:https://my.oschina.net/u/183102/blog/519518