Jump to content

Session Variable and 'foreach' Problem


Recommended Posts

I wonder whether someone can help me please.


I've put together the script below, which allows users to view their saved images in the original folder structure that they were saved in.


<?php session_start(); 



<!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">

  $galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/';
  $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR;
  $descriptions = new DOMDocument('1.0');
  $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 = '';
  <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;

<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/> → <a href = "javascript:document.imagefolders.submit()"> View All Images </a> </div>
<form id="imagefolders" name="imagefolders" class="page" action="gallery.php" method="post">  
   <div id="container">
    <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">
                      <tr class="head">
                        <th class="col-name">
                        <th class="col-size">
                        <th class="col-description">

                      <tr class="directory odd">
                        <td class="col-name">
                          <a href="?path=<?php echo rawurlencode($up); ?>">..</a>
                        <td class="col-size">
                        <td class="col-description">
                      <?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>
                      <?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>
                      <?php endforeach; ?>


I can create the list of correct folders for the user and location, but when I click the folder name to drill down to the indvidual images I receive the following error:


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 17


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


Line 17 is this line: 

$descriptions->load($absGalleryPath . 'files.xml')
and line 54 is:
foreach ($items as $key => $value) {


I know that in their own right, the piece of code which loads 'files.xml' and the code which loads the folders and images work, but it's combining them that creates the issue.


I've done some research online, and I think the issue is to do with 'Session' variables and the 'foreach' array, but I'm not sure how to solve the problem.


I just wondered whether someone could perhaps have a look at this please and let me know where I'm going wrong.


Many thanks and regards

Link to comment
Share on other sites

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

$item isn't being returned from the function, so it can't be used elsewhere in the code (hence why it's an invallid argument for the foreach on the line below).  you also have a $index in the decleration that's never used inside the function...


Your DOM error is because the script either can't find or doesn't have permission to read from the file : "/homepages/2/d333603417/htdocs/development/UploadedFiles/files.xml"

Link to comment
Share on other sites

Hi, many thanks for this.


I think that the issue is more than likely that the script can't find the file. I was having similar issues and error messages whilst building the page.  I solved those issues with getting the file path correct as below. The initial script was used as a defualt script in Aurigma Image Uploader which I'm using, I've obviously now added to this.


$galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/';


I think that the same session variable values need to be passed to the 'foreach' command, but I just don't know how to do this. Could you perhaps let me know how I could do this please?


Thanks and kind regards

Link to comment
Share on other sites

don't know that application, and I'm not sure what or where your looking to use the $_SESSION variables within the foreach.  I will say that $_SSESSION variables have a global scope so you can use them directly anywhere in your code.


Sorry I can't help more than that, hopefully someone else will be better able to help.

Link to comment
Share on other sites

Hi, sadly not, I'm still working on this.


Many thanks for asking though.


Kind regards


Through more testing my question has gone off the topic raised here. I have created another post here:http://www.phpfreaks.com/forums/index.php?topic=358149.0

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.