Php 所选节点未更新

Php 所选节点未更新,php,xml,simplexml,Php,Xml,Simplexml,我正在尝试修改一个拖放上传程序。一旦文件被上传,它就会显示在右边。我想为右侧的每个文件添加一个描述字段。正在通过检查目录来编写我的XML。我能够将每个文件的描述保存到XML中。但是如果我要删除或上传文件到目录,它会用“-”覆盖我的所有描述。我试图做的是上传或删除新项目,而不覆盖我的旧项目描述 PHP-用于删除和上载。此函数用于检查已上载的每个文件的目录并将节点添加到XML中 if ( $handle = opendir( $path_to_image_dir ) ) { while (f

我正在尝试修改一个拖放上传程序。一旦文件被上传,它就会显示在右边。我想为右侧的每个文件添加一个描述字段。正在通过检查目录来编写我的XML。我能够将每个文件的描述保存到XML中。但是如果我要删除或上传文件到目录,它会用“
-
”覆盖我的所有描述。我试图做的是上传或删除新项目,而不覆盖我的旧项目描述

PHP-用于删除和上载。此函数用于检查已上载的每个文件的目录并将节点添加到XML中

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

// Write the XML File to a file.
$xml_generator->asXML('../file.xml');
Description.Php向每个项目添加说明

$filename = "../file.xml";

//value form the pop up description form.
$description = $_POST['description'];

// id of input from the write rightside list. 
$hid = $_POST['fileID'];

$xml->image ->description = $description;

$xml = simplexml_load_file($filename);

foreach($xml->image as $image)
{
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID)
    {
        $image->description = $description;
    }
}

$xml->asXML($filename);
XML

<images>
  <image>
    <id>file0</id>
    <name>image1.jpg</name>
    <height>1435</height>
    <width>2551</width>
    <description>-</description>
  </image>
  <image>
    <id>file1</id>
    <name>Image2.jpg</name>
    <height>1435</height>
    <width>2551</width>
    <description>-</description>
  </image>
</images>

文件0
图1.jpg
1435
2551
-
文件1
图像2.jpg
1435
2551
-

我可以看到您的
描述有3个问题。Php
,首先您有变量
$hid
,该变量应该被称为
$fileID
,您还可以调用
$xml->image->Description=$Description
simplexml\u load\u文件
之前,以及最后一次,您不验证是否设置了fileID和description

因此,您的文件应如下所示:

$filename = "../file.xml";

//value form the pop up description form.
$description = $_POST['description'];
if (!isset($description))
    die("The field description is not set.");

// id of input from the write rightside list. 
$fileID = $_POST['fileID'];
if (!isset($fileID))
    die("The field fileID is not set.");

$xml = simplexml_load_file($filename);

foreach($xml->image as $image)
{
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID)
    {
        $image->description = $description;
    }
}

$xml->asXML($filename);
if ( $handle = opendir( $path_to_image_dir ) )
{
    while (false !== ($file = readdir($handle)))
    {
        if ( is_file($path_to_image_dir.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store" )
        {
            $fileID = 'file'.$i++;
            list( $width, $height ) = getimagesize($path_to_image_dir.'/'.$file);
            $oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]');
            if (count($oldImage) == 0)
            {
                $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 = $oldImage[0];
                $oldImage->name = $file;
                $oldImage->width = $width;
                $oldImage->height = $height;
            }
        }
    }
    closedir($handle);
}

// Write the XML File to a file.
//$xml_generator->asXML('../file.xml');
//In case it saves all in one line and u want 
//to properly format the XML use:
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml_generator->asXML());
echo $dom->save('../file.xml');
您还应该使用
isset
或类似工具来确保POST存在

关于您的第一段代码,所发生的是,在不读取XML的情况下,您已经为每个文件创建了新节点,并将其保存为新的XML覆盖旧的XML,以便保留旧的描述,您应该执行以下操作:

$filename = "../file.xml";

//value form the pop up description form.
$description = $_POST['description'];
if (!isset($description))
    die("The field description is not set.");

// id of input from the write rightside list. 
$fileID = $_POST['fileID'];
if (!isset($fileID))
    die("The field fileID is not set.");

$xml = simplexml_load_file($filename);

foreach($xml->image as $image)
{
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID)
    {
        $image->description = $description;
    }
}

$xml->asXML($filename);
if ( $handle = opendir( $path_to_image_dir ) )
{
    while (false !== ($file = readdir($handle)))
    {
        if ( is_file($path_to_image_dir.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store" )
        {
            $fileID = 'file'.$i++;
            list( $width, $height ) = getimagesize($path_to_image_dir.'/'.$file);
            $oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]');
            if (count($oldImage) == 0)
            {
                $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 = $oldImage[0];
                $oldImage->name = $file;
                $oldImage->width = $width;
                $oldImage->height = $height;
            }
        }
    }
    closedir($handle);
}

// Write the XML File to a file.
//$xml_generator->asXML('../file.xml');
//In case it saves all in one line and u want 
//to properly format the XML use:
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml_generator->asXML());
echo $dom->save('../file.xml');

什么是
echo$image->id
echo$description在Description.Php中提供给您?它们展示了吗?--我这么问是因为你问题中的代码看起来没有错。如果您可以压缩示例,使其可以在没有文件交互的情况下简单地执行,这将有助于改进问题。最后,它是关于XML的,而不是一个文件上传,所以这个标题可能也会吸引一些用户;和描述;对我来说,就是抓取ajax请求控制台日志的值。你可以忽略这些。它实际上什么都没做。对不起,这一定让你感到困惑。关于标题和问题,你可能是对的。谢谢@Prix file0是我的html输入的id。然后我将其与xmlid->file0匹配。因此它更新了xml中唯一的file0部分。这部分已经对我有用了。哦,这是我的语法问题。在发布示例时,我更改了一些变量,我本来打算将其更改为
$fileID
,但在实际代码中的两个位置上都有相同的变量。我试过你的密码。它的工作原理和我的代码一样,但我认为你的方式更简洁。只要改变描述就行了。然而,它并不能解决我最初的问题。当我上传或修改(删除)一个文件时,它仍然被覆盖。然后所有的描述都变成
-
。我真的很感激你的帮助help@Tierendu请参阅更新,假设$xml\u生成器正在读取
。/file.xml
,那么我们可以使用它来匹配现有的图像并简单地更新它,而不是用空描述重新创建整个文件。我感觉这就是问题所在,无法真正想出解决问题的游戏计划。谢谢别担心,我不会让你吊死的。如果它是正确的,我会标记它,否则我会让你知道。我现在正在测试代码。非常感谢,非常感谢。现在效果很好。几天来我一直在想这部分。