无法使用'foreach'命令加载图库图像

时间:2012-04-19 10:52:07

标签: php session-variables

我将以下脚本放在一起,允许用户以原始文件夹结构查看他们保存的上传图片。

更新代码

        <?php session_start(); 

$_SESSION['username']=$_POST['username'];
$_SESSION['locationid']=$_POST['locationid'];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
  //This variable specifies relative path to the folder, where the gallery with uploaded files is located.
  $galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/';

  //let's DEBUG the above assignment 
  if (!is_dir($galleryPath)) { die("No folder exists at $galleryPath!"); } 

  $absGalleryPath = realpath($galleryPath); 

  //let's DEBUG this one too 
  if (!is_dir($absGalleryPath)) { die("No folder exists at $absGalleryPath!"); } 

  $descriptions = new DOMDocument('1.0');

   // DEBUG: let's check for the XML while we're at it 
  //if (!file_exists($absGalleryPath.'files.xml')) { die("No XML found at $absGalleryPath"."files.xml"); } 
  $descriptions->load($absGalleryPath . '/' . 'files.xml'); 


  $items = array();

  for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) {
    $xmlFile = $descriptions->documentElement->childNodes->item($i);
    $path = $xmlFile->getAttribute('name');
    $path = explode('/', $path);

    $t = &$items;
    for ($j = 0; $j < count($path); $j++) {
      if (empty($t[$path[$j]])) {
        $t[$path[$j]] = array();
      }
      $t = &$t[$path[$j]];
    }
    $t['/src/'] = $xmlFile->getAttribute('source');
    $t['description'] = $xmlFile->getAttribute('description');
    $t['size'] = $xmlFile->getAttribute('size');
  }

  $basePath = empty($_GET['path']) ? '' : $_GET['path'];
  if ($basePath) {
    $basePath = explode('/', $basePath);
    for ($j = 0; $j < count($basePath); $j++) {
      $items = &$items[$basePath[$j]];
    }
  }

  $files = array();
  $dirs = array();


  function urlpartencode(&$item, $index) {
    $item = rawurlencode($item);
  }

  foreach ($items as $key => $value) {
    if (isset($value['/src/'])) {
      $value['/src/'] = explode('/', $value['/src/']);
      array_walk($value['/src/'], 'urlpartencode');
      $value['/src/'] = implode('/', $value['/src/']);
      $files[] = array(
        'name' => $key,
        'src' => $value['/src/'],
        'description' => htmlentities($value['description'], ENT_COMPAT, 'UTF-8'),
        'size' => htmlentities($value['size'], ENT_COMPAT, 'UTF-8')
      ); 
    } else {
      $dirs[] = $key;
    }
  }

  $basePath = empty($_GET['path']) ? '' : $_GET['path'];
  $up = dirname($basePath);
  if ($up == '.') {
    $up = '';
  }

  sort($files);
  sort($dirs);
?>
<head>
  <title>View Image Folders</title> 
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link href="Styles/style.css" rel="stylesheet" type="text/css" />
  <script src="Libraries/jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
   <style type="text/css">
<!--
.style1 {
    font-size: 14px;
    margin-top: 5px;
    margin-right: -50px;
}

-->
  </style>
<body style="font-family: Calibri; color:  #505050; margin-right: 160px; margin-left: -180px;">
<div align="right" class="style1"> <a href = "index.php" /> Add Images <a/> &rarr; <a href = "javascript:document.imagefolders.submit()"> View All Images </a> </div>
<form id="imagefolders" name="imagefolders" class="page" action="gallery.php" method="post" enctype="application/x-www-form-urlencoded">  
   <div id="container">
  </div>
    <div id="center">
      <div class="aB">
        <div class="aB-B">
          <?php if ('Uploaded files' != $current['title']) :?>
          <?php endif;?>
          <div class="demo">
          <input name="username" type="hidden" id="username" value="IRHM73" />
          <input name="locationid" type="hidden" id="locationid" value="1" /> 
            <div class="inner">
              <div class="container">
                <div class="gallery">
                  <table class="gallery-link-table" cellpadding="0" cellspacing="0">
                    <thead>
                      <tr class="head">
                        <th class="col-name">
                          Name
                        </th>
                        <th class="col-size">
                          Size
                        </th>
                        <th class="col-description">
                          Description
                        </th>
                      </tr>
                    </thead>

                    <tbody>
                      <tr class="directory odd">
                        <td class="col-name">
                          <a href="?path=<?php echo rawurlencode($up); ?>">..</a>
                        </td>
                        <td class="col-size">
                        </td>
                        <td class="col-description">
                        </td>
                      </tr>
                      <?php $i = 1; ?>

                      <?php foreach ($dirs as $dir) : ?>
                      <tr class="directory <?php $i++; echo ($i % 2 == 0 ? 'even' : 'odd'); ?>">
                        <td><a href="?path=<?php echo rawurlencode(($basePath ? $basePath . '/' : '') . $dir); ?>"><?php echo htmlentities($dir, ENT_COMPAT, 'UTF-8'); ?></a></td>
                        <td>Folder</td>
                        <td></td>
                      </tr>
                      <?php endforeach; ?>

                      <?php foreach ($files as $file) : ?>
                      <tr class="<?php $i++; echo ($i % 2 == 0 ? 'even' : 'odd'); ?>">
                        <td><a target="_blank" href="<?php echo $galleryPath . $file['src']; ?>"><?php echo htmlentities($file['name'], ENT_COMPAT, 'UTF-8'); ?></a></td>
                        <td><?php echo htmlentities($file['size'], ENT_COMPAT, 'UTF-8'); ?></td>
                        <td><?php echo htmlentities($file['description'], ENT_COMPAT, 'UTF-8'); ?></td>
                      </tr>
                      <?php endforeach; ?>
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
  </div>
        </div>
      </div>
    </div>
</form>
</body>
</html>

我可以正确显示文件夹,但是当我点击文件夹以深入查看单个图像时,我收到以下错误:

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "/homepages/2/d333603417/htdocs/development/UploadedFiles/files.xml" in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 25 Warning: Invalid argument supplied for foreach() in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 65

第25行是这行$descriptions->load($absGalleryPath . '/' . 'files.xml'); and line 65 is this: foreach($ items as $ key =&gt; $ value){`

我的初始脚本没有使用会话变量,我没有任何问题。但是我现在需要包含这些,所以我确信这些与foreach命令之间存在冲突。我已经做了很多研究,看看是否有其他人有类似的问题,但找不到任何东西。

我只是想知道是否有人可以看看这个并让我知道我错在哪里。

非常感谢和问候

3 个答案:

答案 0 :(得分:2)

错误1

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "/homepages/2/d333603417/htdocs/development/UploadedFiles/files.xml"

问题的根源是$descriptions->load($absGalleryPath . '/' . 'files.xml');

尝试加载您不确定存在或可读的文件是不好的做法

这样的事情会更好,并确保你抓住异常

$file = $absGalleryPath . '/' . 'files.xml' ;
if(!is_readable($file) || !file_exists($file))
{
 throw new Exception("Missing XML File");
}

错误2

Warning: Invalid argument supplied for foreach() in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 65

一个。由于您无法加载第一个xml文档...尝试继续此过程会导致此错误

B中。 foreach ($items as $key => $value) {所以它会在填充或不填充的地方独立运行

℃。太多的引用会使您的代码更难以理解和调试......

结论

我认为你应该发布files.xml的内容,你会惊讶于它创新的解决方案以及人们想出的有效代码

答案 1 :(得分:0)

我认为Invalid argument supplied for foreach()与您的foreach ($items as $key => $value)有关。因此,请将var_dump($items);放在该foreach之前,以查看items变量的完全转储。好像它甚至都不是阵列。

答案 2 :(得分:0)

  1. 您的foreach循环没有得到正确的数据,因为您愿意这样做。请使用var_dump来检查您获得的数据。
  2. 查看XML
  3. 的路径