动态php xml图像文件属性重新定义错误

动态php xml图像文件属性重新定义错误,php,xml,labels,Php,Xml,Labels,我是一名网络开发新手,尝试通过php制作xml,以便在flash gallery中使用,在检查php文件时出现以下错误: 此页面包含以下错误: 第84列第3行出错:重新定义了属性THUMB 下面是第一个错误之前的页面呈现。 代码如下: <?php // Set which extensions should be approved in the XML file $extensions = array ( 'jpg', 'JPG', 'png', 'PNG', 'gif

我是一名网络开发新手,尝试通过php制作xml,以便在flash gallery中使用,在检查php文件时出现以下错误:

此页面包含以下错误: 第84列第3行出错:重新定义了属性THUMB 下面是第一个错误之前的页面呈现。

代码如下:

    <?php
// Set which extensions should be approved in the XML file
$extensions = array
(
  'jpg', 'JPG',
  'png', 'PNG',
  'gif', 'GIF',
  'bmp', 'BMP'
);

// Echo the header (XML)
header("Content-Type: text/xml");

// Prepare the XML file
echo '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\r\n";
echo '<GALLERY COLUMNS="5" XPOSITION="30" YPOSITION="30" WIDTH="100" HEIGHT="100">' . "\r\n";
$gallery = '<IMAGE FULL="';

// Get the files from the current directory
if (file_exists ("full_images/"))
{
  if (is_dir ("full_images/"))
  {
    $dh = opendir ("full_images/") or die (" Directory Open failed !");

    // Prepare the images array
    $imgs = array();
    while ($file = readdir ($dh))
    {
      // Only get the files ending with the correct extension
      if ( in_array( substr($file, -3), $extensions ) )
      {
        array_push($imgs, $file);
      }
    }
  closedir ($dh);
  }

  // Sort the array
  sort($imgs);

  foreach ($imgs as $img)
  {
    // Return all images in XML format
    $gallery.= 'full_image/' .$img . '" ' . 'THUMB="';
  }
}
// Get the files from the current directory
if (file_exists ("thumbs/"))
{
  if (is_dir ("thumbs/"))
  {
    $dh = opendir ("thumbs/") or die (" Directory Open failed !");

    // Prepare the images array
    $thumbs = array();
    while ($file = readdir ($dh))
    {
      // Only get the files ending with the correct extension
      if ( in_array( substr($file, -3), $extensions ) )
      {
        array_push($thumbs, $file);
      }
    }
  closedir ($dh);
  }

  // Sort the array
  sort($thumbs);

  foreach ($thumbs as $thumb)
  {
    // Return all images in XML format
    $gallery.= 'thumbs/' . $thumb;
  }
}
$gallery.= '" />';
$gallery.= "\r\n";
echo $gallery;
echo "</GALLERY>";
?>

我哪里做错了?解决办法是什么?谢谢

foreach ($imgs as $img)
{
    // Return all images in XML format
    $gallery.= 'full_image/' .$img . '" ' . 'THUMB="';
}
此代码部分添加属性
THUMB
的次数与$imgs中的键数相同

您必须始终创建新的图像子元素

这将是正确的代码(假设thumb和image具有相同的名称)


从技术上讲,错误消息中给出了您的问题。实际上,问题是一次做的事情太多了。相反,要简化。例如,从扩展名筛选的目录中获取文件已经在很大程度上解决了,您只需添加一些小代码:

class ListExtensions extends FilterIterator
{
    private $extensions;

    public function __construct($path, array $extensions = array()) {
        $this->extensions = $extensions;
        parent::__construct(new DirectoryIterator($path));
    }

    /**
     * @link http://php.net/manual/en/filteriterator.accept.php
     * @return bool true if the current element is acceptable, otherwise false.
     */
    public function accept() {
        /* @var $file SplFileInfo */
        $file = $this->getInnerIterator()->current();
        return in_array($file->getExtension(), $this->extensions);
    }
}
此类允许您轻松遍历具有指定扩展名的文件。因为它都是封装的,所以您的代码更加模块化。例如,仅在一个部分中,您专注于目录列表和筛选,而不是在另一个部分中的一个部分中

与生成XML类似。这方面的库也已经存在。它负责创建文档,因为它知道XML,所以您不能犯那么多错误,即使这样,您也会更早地被注意到

$extensions = array
(
    'jpg', 'JPG',
    'png', 'PNG',
    'gif', 'GIF',
    'bmp', 'BMP'
);

$largeFiles = new ListExtensions($pathFullImages, $extensions);
$thumbFiles = new ListExtensions($pathThumbImages, $extensions);

$pairs = new MultipleIterator();
$pairs->attachIterator($largeFiles);
$pairs->attachIterator($thumbFiles);

$gallery = new SimpleXMLElement('<GALLERY COLUMNS="5" XPOSITION="30" YPOSITION="30" WIDTH="100" HEIGHT="100"/>');

foreach ($pairs as $pair)
{
    $image = $gallery->addChild('IMAGE');

    $image['FULL']  = "full_image/$pair[0]";
    $image['THUMB'] = "thumbs/$pair[1]";
}

// Serve XML incl. header
header("Content-Type: text/xml");
$gallery->asXML('php://output');
$extensions=array
(
"jpg","jpg",,
'巴布亚新几内亚','巴布亚新几内亚',
“gif”、“gif”,
“bmp”,“bmp”
);
$largeFiles=新的ListExtensions($pathFullImages,$extensions);
$thumbFiles=新的ListExtensions($pathThumbImages,$extensions);
$pairs=新的乘法器();
$pairs->attachIterator($largeFiles);
$pairs->attachIterator($thumbFiles);
$gallery=新的SimpleXMLElement(“”);
foreach($pairs作为$pair)
{
$image=$gallery->addChild('image');
$image['FULL']=“FULL_image/$pair[0]”;
$image['THUMB']=“thumbs/$pair[1]”;
}
//服务XML,包括标题
标题(“内容类型:text/xml”);
$gallery->asXML($gallery)php://output');

从问题中突出显示的语法可以看出,您编写的代码不多,但字符串太多。我会说这是你要问的“我哪里做错了”的部分。解决方法是修复你所犯的小错误。关闭字符串,而不是将其保持打开状态。@M8R-1jmw5r只有语法高亮显示不起作用……如果不是这样,则错误消息非常准确地告诉您出了什么问题。解决方案是不向同一元素添加两次具有相同名称的属性。XML不支持这一点。@M8R-1jmw5r确切地说,现在解释一下他在PHP代码中的错误在哪里。错误是因为同时做了多件事情。这很复杂,而且会导致更多的错误。是的,这肯定会解决问题;但我真的不确定TO是否会理解你在做什么;)(我的代码只修复了问题,没有重写所有内容…@bwoebi:好吧,至少您可以创建一个函数,将目录读取到数组中。原始代码增加了一倍,这意味着,您需要维护相同的代码两次,同时不做任何区别。我完全不在这里,对不起。它现在给了我这个错误:致命错误:在第10行的/…/../public\u html/flash\u gallery/backhand.php中找不到类'ListExtensions',正如我之前提到的,我是专门针对php和xml进行web开发的新手,所以如果您能给我一个更简要地描述此代码的链接,我将不胜感激?谢谢。@MuhammadAli您自然也必须包含上面的代码(以
class
..)开头)我已经这样做了,但它只是向我显示了一大堆错误:oWell我尝试了你的代码,现在它正确地显示了标签(谢谢),但没有吐出错误。我的意思是它根本就没有回音。D:是的,大拇指和完整的图像有相同的名字。”“你看……它根本没有与$gallery产生共鸣。@MuhammadAli哦,有一个打字错误……(
$images
而不是
$imgs
)现在应该修复了。非常感谢你的耐心。它现在工作得很好:)
$extensions = array
(
    'jpg', 'JPG',
    'png', 'PNG',
    'gif', 'GIF',
    'bmp', 'BMP'
);

$largeFiles = new ListExtensions($pathFullImages, $extensions);
$thumbFiles = new ListExtensions($pathThumbImages, $extensions);

$pairs = new MultipleIterator();
$pairs->attachIterator($largeFiles);
$pairs->attachIterator($thumbFiles);

$gallery = new SimpleXMLElement('<GALLERY COLUMNS="5" XPOSITION="30" YPOSITION="30" WIDTH="100" HEIGHT="100"/>');

foreach ($pairs as $pair)
{
    $image = $gallery->addChild('IMAGE');

    $image['FULL']  = "full_image/$pair[0]";
    $image['THUMB'] = "thumbs/$pair[1]";
}

// Serve XML incl. header
header("Content-Type: text/xml");
$gallery->asXML('php://output');