【问题标题】:PHP & DOM - get number of nodes, delete oldest and add new one as firstPHP & DOM - 获取节点数,删除最旧的并添加新的作为第一个
【发布时间】:2011-08-26 20:22:25
【问题描述】:

我有带有简单 PHP 上传器的 Flash 照片库。

在 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>

有什么建议吗?

附言。对不起我的英语不好:)

问候,阿图尔。

【问题讨论】:

    标签: php xml dom


    【解决方案1】:

    为什么不使用 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 文件中的不同点开始,您将需要存储更多信息。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多