【问题标题】:Last XML Items Isn't Removable From The XML最后一个 XML 项不能从 XML 中删除
【发布时间】:2013-07-17 20:38:38
【问题描述】:

我有两个具有相同逻辑的 php 文档。一个文档是“uploader.php”,一旦文件上传就写入 xml。另一个文档是“modifier.php”,一旦文件被删除,它就会写入 xml。我对这个逻辑有两个问题。第一个问题是删除 xml 列表中的最后一项。它不会删除最后一项,还会复制倒数第二项。第二个问题是它在我的“uploader.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.
}

【问题讨论】:

  • 你删除图片的代码是什么?为什么不使用图像哈希而不是顺序 id ?为什么不使用 MySQL 呢?
  • 我在客户端使用这个 XML 文档和许多其他 XML 文档。我已经弄清楚了那个代码。出于这个原因,我认为使用 XML 会更容易。
  • 你的删除图片代码是什么?
  • 我会及时发布删除代码。谢谢你的帮助。
  • 我刚刚发布了删除代码。

标签: php xml simplexml


【解决方案1】:

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

改变这一行:

$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 请求更改为它不会删除的其他内容一样。

【讨论】:

  • @Tierendu 再看看我已经更新了代码以更正删除文件的问题。
  • 嗨@Prix,我正在使用您最新的代码示例,但它不会从XML 中删除文件,而是转到Didn't manage to delete the file: 语句。我一定做错了什么。我只是想向您更新正在发生的事情。一如既往地感谢您的帮助。
  • @Tierendu 文件不存在或者您无权删除该文件,因为它在unlink 上失败,您可以尝试使用file_exists 或将此@ 987654322@ 在您的echo "Didn't... 下方,看看它告诉您什么错误。
  • @Tierendu 还有$_POST['file'] 的内容是什么,你能更新一下print_r($_POST['file']);
  • @Tierendu 只需在上面的代码中注意到// very top code goes here.,除非您有要更新的新文件,否则您不需要运行顶级代码,并且您应该明确考虑迁移到您的设置开始的 MySQL真的搞砸了,甚至你都很难。
猜你喜欢
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 2020-12-03
  • 1970-01-01
相关资源
最近更新 更多