所选节点未更新


Selected node not updating

我正在尝试修改一个拖放上传器。一旦上传了文件,它就会显示在右侧。我想在右侧为每个文件添加一个描述字段。我的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');

说明。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>

我可以看到Description.Php有三个问题,首先是变量$hid本应被称为$fileID,而且在simplexml_load_file之前还调用了$xml->image ->description = $description;,最后没有验证是否设置了fileID和描述。

所以你的文件应该是这样的:

$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);

您还应该使用isset或类似的方法来确保POST的存在。

对于您的第一个代码,所发生的情况是,在不读取现有XML的情况下,您为每个文件一遍又一遍地创建新节点,并将其保存为覆盖旧文件的新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" )
        {
            $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');