0

我是网络开发的新手,并试图通过 php 制作 xml 以在我的 flash 库中使用,并在检查我的 php 文件时收到此错误:

此页面包含以下错误: 第 3 行第 84 列的错误:重新定义属性 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>";
?>

我哪里错了?解决方案是什么?谢谢你。

4

2 回答 2

0
foreach ($imgs as $img)
{
    // Return all images in XML format
    $gallery.= 'full_image/' .$img . '" ' . 'THUMB="';
}

此代码部分添加属性THUMB的次数与 $imgs 中的键数一样多。

您必须始终创建一个新的 Image 子元素。

这将是正确的代码(假设拇指和图像具有相同的名称)

<?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 = '';

// 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);
}
// 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);

  for ($i = 0; $i < count($imgs); $i++)
  {
    // Return all images in XML format
    $gallery .= '<IMAGE FULL="full_image/'.$imgs[$i].'" THUMB="thumbs/' . $thumbs[$i].'"/>'."\r\n";
  }
}
echo $gallery;
echo "</GALLERY>";
?>
于 2013-04-15T19:45:29.777 回答
0

从技术上讲,您的问题在错误消息中给出。实际上,问题在于一次做太多事情。而是简化。例如从通过扩展过滤的目录中获取文件已经大部分解决了,您只需要添加一些小代码:

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');
于 2013-04-15T20:02:25.247 回答