所选节点未更新

时间:2013-07-12 23:38:38

标签: php xml simplexml

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

1 个答案:

答案 0 :(得分:0)

我可以看到您的Description.Php存在3个问题,首先您应该调用$hid变量$fileID,然后在{{$xml->image ->description = $description;之前调用simplexml_load_file 1}}并且最后你不验证是否设置了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覆盖旧的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');