PHP 与DOM - 获取节点数,删除最旧的节点并添加新的节点作为第一个
我有 Flash 照片库和简单的 PHP 上传器。
在 uploader.php 文件中,我添加了将最后 20 张添加的照片存储到 xml 文件中的代码。
用于将数据保存在xml文件中的脚本我已经完成了,xml结构如下所示:
<images>
<image thumb="/content/photos/tn_PICTURE1.jpg" image="/content/photos/PICTURE1.jpg" name="PICTURE1"/>
<image thumb="/content/photos/tn_PICTURE2.jpg" image="/content/photos/PICTURE2.jpg" name="PICTURE2"/>
<image thumb="/content/photos/tn_PICTURE3.jpg" image="/content/photos/PICTURE3.jpg" name="PICTURE3"/>
...
<image thumb="/content/photos/tn_PICTURE20.jpg" image="/content/photos/PICTURE20.jpg" name="PICTURE20"/>
</images>
uploader.PHP文件的部分:
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->load("new_files.xml");
$root = $domtree->documentElement;
$currentImage = $domtree->createElement("image");
$currentImage = $root->appendChild($currentImage);
$thumb = $domtree->createAttribute("thumb");
$currentImage->appendChild($thumb);
$thumbValue = $domtree->createTextNode($thumbnail);
$thumb->appendChild($thumbValue);
$imagelink = $domtree->createAttribute("image");
$currentImage->appendChild($imagelink);
$imageValue = $domtree->createTextNode($fullpath);
$imagelink->appendChild($imageValue);
$imagename = $domtree->createAttribute("name");
$currentImage->appendChild($imagename);
$imagenameValue = $domtree->createTextNode($imageName);
$imagename->appendChild($imagenameValue);
$domtree->save("new_files.xml");
我想做什么?
当xml文件中的图片超过20张时,我希望该脚本会自动删除最后一张(最旧的),并将最新的放在第一位。所以结果将如下所示:
<images>
<image thumb="/content/photos/tn_PICTURE21.jpg" image="/content/photos/PICTURE21.jpg" name="PICTURE21"/>
<image thumb="/content/photos/tn_PICTURE1.jpg" image="/content/photos/PICTURE1.jpg" name="PICTURE1"/>
<image thumb="/content/photos/tn_PICTURE2.jpg" image="/content/photos/PICTURE2.jpg" name="PICTURE2"/>
...
<image thumb="/content/photos/tn_PICTURE19.jpg" image="/content/photos/PICTURE19.jpg" name="PICTURE19"/>
</images>
有什么建议吗?
诗。抱歉我的英语不好:)
问候,阿图尔。
I have flash photo gallery with simple PHP uploader.
In uploader.php file I have added code which store last 20 added photos into xml file.
The script for saving data in xml file I have already done, and xml structure looks like this:
<images>
<image thumb="/content/photos/tn_PICTURE1.jpg" image="/content/photos/PICTURE1.jpg" name="PICTURE1"/>
<image thumb="/content/photos/tn_PICTURE2.jpg" image="/content/photos/PICTURE2.jpg" name="PICTURE2"/>
<image thumb="/content/photos/tn_PICTURE3.jpg" image="/content/photos/PICTURE3.jpg" name="PICTURE3"/>
...
<image thumb="/content/photos/tn_PICTURE20.jpg" image="/content/photos/PICTURE20.jpg" name="PICTURE20"/>
</images>
The part of uploader.PHP file:
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->load("new_files.xml");
$root = $domtree->documentElement;
$currentImage = $domtree->createElement("image");
$currentImage = $root->appendChild($currentImage);
$thumb = $domtree->createAttribute("thumb");
$currentImage->appendChild($thumb);
$thumbValue = $domtree->createTextNode($thumbnail);
$thumb->appendChild($thumbValue);
$imagelink = $domtree->createAttribute("image");
$currentImage->appendChild($imagelink);
$imageValue = $domtree->createTextNode($fullpath);
$imagelink->appendChild($imageValue);
$imagename = $domtree->createAttribute("name");
$currentImage->appendChild($imagename);
$imagenameValue = $domtree->createTextNode($imageName);
$imagename->appendChild($imagenameValue);
$domtree->save("new_files.xml");
What I want to do?
When is more than 20 pictures in xml file, then I want that script will automaticaly deleting last one (oldest), and put the newest one as first. So the result will looks like this:
<images>
<image thumb="/content/photos/tn_PICTURE21.jpg" image="/content/photos/PICTURE21.jpg" name="PICTURE21"/>
<image thumb="/content/photos/tn_PICTURE1.jpg" image="/content/photos/PICTURE1.jpg" name="PICTURE1"/>
<image thumb="/content/photos/tn_PICTURE2.jpg" image="/content/photos/PICTURE2.jpg" name="PICTURE2"/>
...
<image thumb="/content/photos/tn_PICTURE19.jpg" image="/content/photos/PICTURE19.jpg" name="PICTURE19"/>
</images>
Any suggestions?
Ps. Sorry for my bad english :)
Regards, Artur.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用 SimpleXML 对象?如果您只处理 XML,那么使用起来要容易得多。
之后,您需要做的就是加载 XML 文件并执行如下操作:
唯一的问题是,如果您再次上传,它将从 XML 对象的顶部开始。如果您希望能够从 XML 文件中的不同点开始,则需要存储更多信息。
希望有帮助。
Why not use the SimpleXML object? It is far easier to use if you are just dealing with XML.
After that, all you need to do is load up the XML file and do something like this:
The only problem is, if you upload again, it will start from the top of your XML object. If you want to be able to start from a different point in your XML file, you are going to need to store more information.
Hope that helps.