【发布时间】:2013-07-19 05:58:58
【问题描述】:
我正在尝试修改拖放上传器。上传文件后,它会显示在右侧。我想在右侧为每个文件添加一个描述字段。我的 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>
【问题讨论】:
-
echo $image->id;和echo $description;在 Description.Php 中为您提供了什么?它们会显示吗? ---我只是问,因为您问题中的代码看起来并没有那么错。如果您可以压缩示例,那么它可以在没有文件交互的情况下简单地执行,这将有助于改善问题。最后是关于 XML 而不是文件上传,所以标题也可能会吸引一些用户。 -
回声 $image->id;并回显$描述;对我来说,将 ajax 请求的值获取到控制台日志。你可以忽略那些。它并没有真正做任何事情。对不起,这一定让你感到困惑。你可能对标题和问题是正确的。谢谢!
-
@Prix file0 是我的 html 输入的 id。然后我将其与 xml id->file0 匹配。所以它更新了 xml 中唯一的 file0 部分。这部分已经对我有用了。