实际上这个的技术含量没有,但是为了完整性,自己还是上传一个上传信息的程序代码吧,而且这个可以直接拿来用的,尤其是上传photo的这一个部分,并且注意要怎么建立两个页面的联系,即上传了photo就可以很及时的显示出来。
上传信息的 首页:
<!--add_goods.php:添加商品--------------------------------------->
<html>
<head>
<title>添加商品</title>
<script language="JavaScript">
//获得焦点
function NameGetFocus()
{
document.frmAdd.goods_name.focus();
}
//检查输入合法性
function CheckValid()
{
if(document.frmAdd.goods_name.value=="")
{
alert("Please input goods name!");
document.frmAdd.goods_name.focus();
return false;
}
return true;
}
</script>
</head>
<body onload="NameGetFocus();">
<?php include("head.html")?>
<h1 align="center">添加新商品</h1>
<table width="60%" border="1" align="center" bgcolor="#F0F0F0">
<form method="POST" name="frmAdd" action="check_goods.php">
<tr>
<td align="center" colspan=2><img src="images/logo.gif" width="250" height="70"></td>
</tr>
<tr>
<td width="30%" height="29">名称:</td>
<td colspan="2" height="29" width="78%">
<input type="text" name="goods_name" size="40" value=<?php if(isset($goods_name)) echo $goods_name; else echo "";?> >
</td>
</tr>
<tr>
<td width="30%" height="29">图片:</td>
<td colspan="2" height="29" width="78%">
<?php
if(isset($_GET['photo_dir_name']))
{
$photo_dir_name=$_GET['photo_dir_name'];
echo "<a href='upload_image/$photo_dir_name' target='_blank'>【 $photo_dir_name 】</a>";
echo "<input type='hidden' name='photodir' value=$photo_dir_name>";
}
else
{
echo "【未上传图片】";
echo "<input type='hidden' name='photodir' value=''>";
}
?>
<a href="upload_photo.php?goods_name="">上传图片</a>
<input type="hidden" name="photodir" value=<?php if(isset($photo_dir_name)) echo $photo_dir_name ?>>
</td>
</tr>
<tr>
<td width="30%" height="29">商品介绍:</td>
<td colspan="3" valign="middle" align="left">
<textarea rows="6" name="description" cols="55" wrap="VIRTUAL"></textarea>
</td>
</tr>
<tr>
<td width="30%" height="29">单位:</td>
<td colspan="2" height="29" width="78%">
<input type="text" name="uint" size="40">
</td>
</tr>
<tr>
<td width="30%" height="29">初始价格:</td>
<td colspan="2" height="29" width="78%">
<input type="text" name="init_price" size="40">
</td>
</tr>
<tr>
<td width="30%" height="29">结束时间:</td>
<td colspan="2" height="29" width="78%">
<input type="text" name="endtime" size="40">
</td>
</tr>
<tr>
<td colspan="3" height="24">
<div align="center"><font color="#00FF00">
<input type="submit" name="cmdadd" value="确定" onclick="return CheckValid();">
<input type="reset" value="重 写" name="cencel">
</font></div>
</td>
</tr>
</form>
</table>
</body>
</html>
上传信息的photo这个过程:
<!--upload_photo.php:上传商品图片--------------------------------------->
<html>
<head>
<title>上传图片</title>
</head>
<body>
<?php
//如果点击"上传",则执行此php代码部分
if (isset($_FILES['photo_dir']))
{
$photo_dir=$_FILES["photo_dir"];
//如果没有上传文件,则退出程序
if ($photo_dir=="")
{
echo "<script language=\"JavaScript\">";
echo "alert(\"你没有上传任何文件\");";
echo "history.back();";
echo "</script>";
exit;
}
// //上传文件到本目录下的upload_image文件夹中
// $fp=opendir("./upload_image");
// if ($fp==0)
// {
// mkdir("./upload_image"); //若目录不存在,则新建一个
// $fp=opendir("./upload_image"); //取得目录handle
// }
if ($_FILES["photo_dir"]["error"] == 0) {
move_uploaded_file($_FILES["photo_dir"]["tmp_name"],
"upload_image/" . $_FILES["photo_dir"]["name"]);
$photo_dir_name = $_FILES["photo_dir"]["name"];
//将临时文件复制到upload_image目录下
// $up=copy("$photo_dir","upload_image/$photo_dir_name");
// if($up==1)
// {
// 获取带路径的文件名
// $photo_dir_name="$photo_dir_name";
// 写入内容
// unlink ($photo_dir); //从临时文件夹中删除档案$photo_dir
// closedir ($fp); //关闭目录handle
//
// 返回添加商品页面
echo "<script language='javascript'>";
echo "alert(\"上传成功!\");";
echo " location='add_goods.php?photo_dir_name=$photo_dir_name'";
echo "</script>";
// }
// 若上传失败,则退出程序
// else
// {
// echo "文件上传失败.";
// exit;
// }
}
else
{
echo ($_FILES["photo_dir"]["error"]);
exit;
}
}
?>
<table width="60%" border="1" cellspacing="0" cellpadding="0" align="center" BGCOLOR="#F0F0F0">
<tr bgcolor="#6699FF">
<td colspan=2>
<div align="center" class="white12">上传图片</div>
</td>
</tr>
<tr>
<td>
<!-- 注意:这里必须加上‘enctype="multipart/form-data" ’,否则不会产生上传动作-->
<form action="upload_photo.php" method="post" enctype="multipart/form-data" name="UL">
图片源文件:
</td>
<td>
<input type="file" name="photo_dir" size="15" accept="upload_image/x-png,image/gif,image/jpeg">
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="Submit" name="upload" value="上传">
</td>
</tr>
</form>
</table>
</body>
</html>