I have two php documents that has this same logic. One document is "uploader.php, which writes to the xml once file is upload. The other document is "modifier.php", which writes to the xml once file the is removed. I have two issue with this logic. First issue is deleting last item in the xml list. It doesn't delete the last item and also duplicates the second to last items. Second issue is with it is logging a error on my "uploader.php".
$xml_generator = simplexml_load_file("../file.xml");
if ( $handle = opendir( $path_to_image_dir ) )
{
while (false !== ($file = readdir($handle)))
{
if ( is_file($path.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store" )
{
$fileID = $i++;
list( $width, $height ) = getimagesize($path.'/'.$file);
$oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]')[0];
if (!isset($oldImage))
{
$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->name = $file;
$oldImage->width = $width;
$oldImage->height = $height;
}
}
}
closedir($handle);
}
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml_generator->asXML());
echo $dom->save('../file.xml');
Example of the First Issue
Image2.jpg is last item on the list. If I were to delete Image2.jpg, second to last item get duplicated while Image2.jpg remains on XML Document.
<image>
<id>9</id>
<name>Image1.jpg</name>
<width>2551</width>
<height>1435</height>
<description>-</description>
</image>
<image>
<id>10</id>
<name>Image1.jpg</name>
<width>2551</width>
<height>1435</height>
<description>-</description>
</image>
<image>
<id>11</id>
<name>Images2.jpg</name>
<width>612</width>
<height>612</height>
<description>-</description>
</image>
Second Issue is Error Msg .
Undefined offset: 0 in uploader.php on line $oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]')[0];
I think both of these issues are related to the same problem, please help me solve this problem. Thanks!
Delete Code- this code can delete any item except for the last item on the list.
if(isset($_POST['delete'])){
foreach($_POST['file'] as $file) {
if(isset($file)) {
if (unlink($path."/".$file)) {
echo "Delete the file: $file<br />";
if (!empty($_SERVER['HTTP_REFERER'])){
header("Location: " . $_SERVER['HTTP_REFERER']);
} else {
echo "No referrer.";
}
} else {
echo "Didn't manage to delete the file: $file<br />";
}
}
}
// very top code goes here.
}