PHP 与DOM - 获取节点数,删除最旧的节点并添加新的节点作为第一个

发布于 2024-12-01 18:52:42 字数 2203 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2024-12-08 18:52:42

为什么不使用 SimpleXML 对象?如果您只处理 XML,那么使用起来要容易得多。

之后,您需要做的就是加载 XML 文件并执行如下操作:

$uploadedFilesCount = count($uploadedFiledArray);

$currentIndex = 0;
while($totalIndex < $uploadedFilesCount){
    foreach($YourSweetXMLObject->children() as $child){
        $child->attributes()->thumbs = $uploadedFiledArray[$currentIndex]['thumbs'];
        $child->attributes()->image = $uploadedFiledArray[$currentIndex]['image'];
        $child->attributes()->name = $uploadedFiledArray[$currentIndex]['name'];

        $totalIndex++;   
    }
}

唯一的问题是,如果您再次上传,它将从 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:

$uploadedFilesCount = count($uploadedFiledArray);

$currentIndex = 0;
while($totalIndex < $uploadedFilesCount){
    foreach($YourSweetXMLObject->children() as $child){
        $child->attributes()->thumbs = $uploadedFiledArray[$currentIndex]['thumbs'];
        $child->attributes()->image = $uploadedFiledArray[$currentIndex]['image'];
        $child->attributes()->name = $uploadedFiledArray[$currentIndex]['name'];

        $totalIndex++;   
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文