0

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

1 回答 1

0

我可以看到您的 3 个问题,首先您有应该调用Description.Php的变量,并且您还在之前调用,最后您不验证是否设置了 fileID 和 description。$hid$fileID$xml->image ->description = $description;simplexml_load_file

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

$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');
于 2013-07-15T17:12:10.910 回答