最后一个XML项不能从XML中删除 [英] Last XML Items Isn't Removable From The XML

查看:31
本文介绍了最后一个XML项不能从XML中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个php文档具有相同的逻辑。一个文档是"ploader.php",它在上传文件后写入到XML。另一个文档是"Modifier.php",它在文件被删除后写入到XML。我对这个逻辑有两个问题。第一个问题是删除XML列表中的最后一项。它不会删除最后一项,还会复制倒数第二项。第二个问题是在我的"ploader.php"上记录一个错误。

$xml_generator = simplexml_load_file("../file.xml");

if ( $handle = opendir( $path_to_image_dir ) )
{
    while (false !== ($file = readdir($handle)))
    {
        if ( is_file($path.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store" )
        {
            $fileID = $i++;
            list( $width, $height ) = getimagesize($path.'/'.$file);
            $oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]')[0];
            if (!isset($oldImage))
            {
                $image = $xml_generator->addChild('image');
                $image->addChild('id', $fileID);
                $image->addChild('name', $file);
                $image->addChild('width', $width);
                $image->addChild('height', $height);
                $image->addChild('description', '-');
            }
            else
            {
                $oldImage->name = $file;
                $oldImage->width = $width;
                $oldImage->height = $height;
            }
        }
    }
    closedir($handle);
}

$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml_generator->asXML());
echo $dom->save('../file.xml');

第一期示例

Image2.jpg是列表中的最后一项。如果我删除Image2.jpg,则会复制倒数第二个项目,而Image2.jpg保留在XML文档上。

<image>
 <id>9</id>
 <name>Image1.jpg</name>
 <width>2551</width>
 <height>1435</height>
 <description>-</description>
</image>
<image>
 <id>10</id>
 <name>Image1.jpg</name>
 <width>2551</width>
 <height>1435</height>
 <description>-</description>
</image>
<image>
 <id>11</id>
 <name>Images2.jpg</name>
 <width>612</width>
 <height>612</height>
 <description>-</description>
</image>

第二个问题是错误消息

Undefined offset: 0 in uploader.php on line $oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]')[0];

我认为这两个问题都与同一个问题有关,请帮助我解决这个问题。谢谢!

删除代码-此代码可以删除列表中除最后一项之外的任何项。

if(isset($_POST['delete'])){
    foreach($_POST['file'] as $file) {
        if(isset($file)) {
            if (unlink($path."/".$file)) {
                echo "Delete the file: $file<br />";
               if (!empty($_SERVER['HTTP_REFERER'])){
                    header("Location: " . $_SERVER['HTTP_REFERER']);
                } else {
                   echo "No referrer.";
                }
            } else {
                echo "Didn't manage to delete the file: $file<br />";
            }
        }
    }
    // very top code goes here.
}

推荐答案

但是,如果图像可以具有相同或相同的名称,则此方法将失败。如果您为每个上载的图像生成唯一的名称,这样它就不会冲突,这会更好。

更改此行:

$oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]');
if (!isset($oldImage))

收件人:

$oldImage = $xml_generator->xpath('//images/image[name="'.$file.'"]');
if (count($oldImage) == 0)

若要避免通知,请更改此设置:

else
{
    $oldImage->name = $file;

收件人:

else
{
    $oldImage = $oldImage[0];
    $oldImage->name = $file;

在删除文件时,您必须排除该元素,以便它不会复制。

举个例子:

$filename = '../file.xml';
$xml = simplexml_load_file($filename);
if(isset($_POST['delete']))
{
    $deleted = 0;
    foreach($_POST['file'] as $file)
    {
        if(isset($file))
        {
            $image = $xml->xpath("//images/image[name='$file']");
            if (!empty($image))
            {
                if (unlink($path."/".$file))
                {
                    $deleted++;
                    $dom=dom_import_simplexml($image[0]);
                    $dom->parentNode->removeChild($dom);
                    echo "Delete the file: $file<br />";
                    if (!empty($_SERVER['HTTP_REFERER']))
                    {
                        header("Location: " . $_SERVER['HTTP_REFERER']);
                    }
                    else
                    {
                        echo "No referrer.";
                    }
                }
                else
                {
                    echo "Didn't manage to delete the file: $file<br />";
                }
            }
            else
            {
                echo "File not found: $file<br />";
            }
        }
    }
    // Avoid unnecessary saving the file
    if ($deleted > 0)
    {
        $dom = new DOMDocument('1.0');
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($xml->asXML());
        $dom->save($filename);
    }
}

请记住,这还将防止用户删除不存在于XML上的文件,就好像他们将POST请求更改为其他不存在的文件一样。

这篇关于最后一个XML项不能从XML中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆