if (move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile)){ //保存上传的图片到upfile文件夹
echo "上传图片成功";
$imgflag=1;
}
else{
echo "上传图片失败!";
$imgflag=0;
}
}
}
//获取其他表单域
$author=base64_encode($_POST["author"]);
$content=base64_encode(ereg_replace("\r\n","<br>",$_POST["content"]));
$smiles=base64_encode($_POST["smiles"]);
if($_POST["title"]){
$title=base64_encode($_POST["title"]);
}else{
$title=base64_encode("无标题");
}
$addtime=date("Y-m-d");
if($imgflag==1){ //如果有上传图片
$photo=base64_encode($randname);
}else{ //否则将photo元素的值设置为NONE
$photo="NONE";
}
$dom=new DOMDocument('1.0','gb2312'); //指定XML的格式
$dom->load("data.xml"); //加载
$root=$dom->getElementsByTagName("messages"); //获取根节点
$root=$root->item(0);
$last_id=$root->lastChild->firstChild->nodeValue; //获取最后一个message的第一个子节点(即id节点)的值
$id=$last_id+1; //新增消息的id
settype($id,"string"); //将其转换为字符型
$message=$root->appendChild(new DOMElement('message')); //添加message节点
$el_id=$message->appendChild(new DOMElement('id')); //添加message节点的各个子节点
$el_id->appendChild($dom->createTextNode($id));
$el_author=$message->appendChild(new DOMElement('author'));
$el_author->appendChild($dom->createTextNode($author));
$el_title=$message->appendChild(new DOMElement('title'));
$el_title->appendChild($dom->createTextNode($title));
$el_smiles=$message->appendChild(new DOMElement('smiles'));
$el_smiles->appendChild($dom->createTextNode($smiles));
$el_content=$message->appendChild(new DOMElement('content'));
$el_content->appendChild($dom->createTextNode($content));
$el_addtime=$message->appendChild(new DOMElement('addtime'));
$el_addtime->appendChild($dom->createTextNode($addtime));
$el_photo=$message->appendChild(new DOMElement('photo'));
$el_photo->appendChild($dom->createTextNode($photo));
$dom->save("data.xml"); //保存XML
echo "<meta http-equiv=\"refresh\" content=\"2;url=index.php\">\n";
echo "谢谢您的留言,2秒钟返回首页";
}
?>
(3)index.php
本页面用于显示留言信息
<p><a href="https://www.jb51.net/add.php">添加留言</a></p>
<?php
$dom=new DOMDocument('1.0','gb2312');
$dom->load("data.xml"); //加载
$root=$dom->getElementsByTagName("messages");
$root=$root->item(0);
$message=$root->getElementsByTagName("message"); //获取所有message节点
$message_count=$message->length; //计算有多少条留言
echo "当前共有".$message_count."条留言";
if($message_count==0){
echo "暂时没有留言\n";
}else{
?>
<table>
<?php
for($i=$message_count-1;$i>=0;$i--) //我们需要对留言按倒序排列
{
$msg=$message->item($i);
foreach($msg->childNodes as $child) //message节点的各个子节点
{
if($child->nodeName=="id")
{
$id=$child->nodeValue;
}
if($child->nodeName=="author")
{
$author=$child->nodeValue;
}
if($child->nodeName=="title")
{
$title=$child->nodeValue;
}
if($child->nodeName=="smiles")
{
$smiles=$child->nodeValue;
}
if($child->nodeName=="content")
{
$content=$child->nodeValue;
}
if($child->nodeName=="photo")
{
$photo=$child->nodeValue;
}
if($child->nodeName=="addtime")
{
$addtime=$child->nodeValue;
}
}
echo "<tr>";
echo "<td align=left bgcolor=#CCCCFF>";
echo $id.".<img src='smiles/".base64_decode($smiles)."'>".base64_decode($title)." - ".base64_decode($author)." [".$addtime."] ";
if(isset($_SESSION["password"]) && $_SESSION["password"]!="") //如果输入了密码显示删除链接
{
echo "[<a href='del.php?id=".$id."'>删除</a>]";
}
echo "</td></tr>";
echo "<tr><td align=left>".base64_decode($content)."</td></tr>";
if($photo!="NONE")
{
echo "<tr><td align=left><img src='upfile/".base64_decode($photo)."'></td></tr>";
}
}
?>
<?php
}
?>
</table>
<?php
if(isset($_SESSION["password"]) && $_SESSION["password"]!=""){
?>
<p><a href="https://www.jb51.net/logout.php">退出管理</a></p>
<?php
}else{
?>
<p><a href="https://www.jb51.net/login.php">登陆管理</a></p>
<?php
}
?>
(4) 删除留言
<?php
if(isset($_SESSION["password"]) && $_SESSION["password"]!="")
{