Session Variable and 'foreach' Array Issue
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Session Variable and 'foreach' Array Issue

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    14

    Session Variable and 'foreach' Array Issue

    I wonder whether someone can help me please.

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

    Code:
    <?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 
    
      $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 = '';
      }
      
      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">  
       <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>
    </form>
    </body>
    </html>
    I can create the list of folders correctly, 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: [
    Code:
    $descriptions->load($absGalleryPath . 'files.xml')
    and line 54 is: foreach
    Code:
    ($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

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,697
    The "line 54 error" is caused by the "failure to load" error. If you fix "failure to load", the other error should Just Go Away(tm).

    So, the problem is that the session variables aren't set to anything meaningful ... OR, that the server can't find folders that correspond to the path indicated. You might try this (unless you don't want some site visitors to see errors messages, that is):
    PHP Code:

      $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) . DIRECTORY_SEPARATOR;

    //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'); 
    That should tell you something.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Hi, many thanks for taking the time to reply to my post.

    I've tried your suggestion and I receive the following error:

    No XML found at /homepages/2/d333603417/htdocs/development/UploadedFiles/files.xml
    I must admit, I'm really not sure what to mkae of it though because I know the file path is correct.

    Could you tell me please have yu any ideas?

    Kind regards

  4. #4
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by IRHM73 View Post
    I must admit, I'm really not sure what to mkae of it though because I know the file path is correct.
    ...meaning, that is the path you meant to write?
    or that you went there directly (via FTP or whatever) and actually confirmed that the file exists?

    PHP checked, and can't find the file. So, it's either not there, or php doesn't have the necessary permissions to access it (add error_reporting( -1 ); at the beginning of your script; you should see an error message about permissions in the latter case).

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,697
    Quote Originally Posted by IRHM73 View Post
    I must admit, I'm really not sure what to mkae of it though because I know the file path is correct.
    I would agree with traq --- at this point, I usually have one of these moments again ...
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Hi, many thanks to both of you for helping me out with this.

    The file path i.e. I went directly to the files and they are there.

    I ran the
    error_reporting( -1 );
    as you suggested and amongst quite a few errors, there was one for both of my session variables that read:

    Notice: Undefined index: username in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 3
    and

    Notice: Undefined index: locationid in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 4
    The really strange about his is that I have a 'gallery.php' script that uses more or less the same lines to recover the data and it works fine.

    I've aded this below:

    Code:
    <?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 
    
      $galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/';
        
      $thumbnailsPath = $galleryPath . 'Thumbnails/'; 
        
      $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 
        
      $descriptions = new DOMDocument('1.0'); 
      $descriptions->load($absGalleryPath . 'files.xml'); 
    ?>
    <head> 
      <title>Gallery</title> 
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
      <link href="Libraries/fancybox/jquery.fancybox-1.3.1.css" rel="stylesheet" type="text/css" /> 
      <link href="Styles/style.css" rel="stylesheet" type="text/css" /> 
      <!--[if IE]>   
      <link href="Styles/ie.css" rel="stylesheet" type="text/css" /> 
      <![endif]-->
      <script src="Libraries/jquery/jquery-1.4.3.min.js" type="text/javascript"></script> 
      <script src="Libraries/fancybox/jquery.fancybox-1.3.1.pack.js" type="text/javascript"></script> 
      <script type="text/javascript"> 
      
      $(function() { $('a.fancybox').fancybox(); }); 
        
      </script> 
      <style type="text/css">
    <!--
    .style1 {
    	font-size: 14px;
    	margin-right: 110px;
    }
    .style4 {font-size: 12px}
    -->
      </style>
    </head>
    <body style="font-family: Calibri; color:  #505050; font-size: 9px; border-bottom-width: thin; margin-top: 5px; margin-left: -476px; margin-right: 1px; margin-bottom: -10px;">
    <div align="right" class="style1"> <a href = "javascript:document.gallery.submit()"/> View Uploaded Images In Folder Structure <a/> &larr; View All Uploaded Images </div>
      <form id="gallery" name="gallery" class="page" action="imagefolders.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"> 
                      <ul class="gallery-image-list"> 
                      <?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) : 
                              $xmlFile = $descriptions->documentElement->childNodes->item($i); 
                              $name = htmlentities($xmlFile->getAttribute('originalname'), ENT_COMPAT, 'UTF-8'); 
    						  $description = htmlentities($xmlFile->getAttribute('description'), ENT_COMPAT, 'UTF-8'); 
    						  $folder = htmlentities($xmlFile->getAttribute('folder'), ENT_COMPAT, 'UTF-8'); 
                              $source = $galleryPath . rawurlencode($xmlFile->getAttribute('source')); 
                              $thumbnail = $thumbnailsPath . rawurlencode($xmlFile->getAttribute('thumbnail')); 
                      ?>
                        <li class="item"> 
                          <a class="fancybox" target="_blank" rel="original" href="<?php echo $source; ?>"><img class="preview" 
                            alt="<?php echo $name; ?>"  src="<?php echo $thumbnail; ?>" /></a></li>
    						<p><span class="style4"><b>Image Description:</b> <?php echo htmlentities($xmlFile->getAttribute('description'));?> <br />
    						  <b>Contained in folder:</b> <?php echo htmlentities($xmlFile->getAttribute('folder'));?> </span><br />  
    					      <?php endfor; ?>
    					      </li>
    				        </p>
                      </ul>
                    </div> 
            </div> 
          </div> 
        </div> 
        </div> 
            <div class="aB-a">        </div> 
          </div> 
        </div> 
      </div> 
      </form> 
    </body> 
    </html>
    Kind regards

  7. #7
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,697
    OK, for additional debugging, do some calls on the following vars:

    $_SESSION['username'];
    $_SESSION['location'];

    and this constant:

    DIRECTORY_SEPARATOR

    I'd suggest either isset() or echo() for the vars, and is_defined for the constant...
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Hi, many thanks for your continued help with this.

    As you were writing your post, I was checking that the variables are being pulled through correctly and they are.

    I must admit I'm not sure how to check the Directory constant, but after tidying up the page a bit and running the script I receive this error:

    Warning: Invalid argument supplied for foreach() in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 51
    I think this goes back to my original post. It's something in the 'foreach' command. I'm note even sure whether this is an issue. But is it possible that I need to include the array in the session? just a thought. Kind regards

  9. #9
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,697
    Quote Originally Posted by IRHM73 View Post
    Hi, many thanks for your continued help with this.
    Lol ... you're just lucky the boss is away from the office this week, that's all

    As you were writing your post, I was checking that the variables are being pulled through correctly and they are.

    I must admit I'm not sure how to check the Directory constant,
    My bad! (there's a DOH!) You need "defined", as there is no is_defined:
    PHP Code:
    if (!defined('DIRECTORY_SEPARATOR')) { echo "It's not defined!"; } 
    but after tidying up the page a bit and running the script I receive this error:

    I think this goes back to my original post. It's something in the 'foreach' command. I'm note even sure whether this is an issue. But is it possible that I need to include the array in the session? just a thought. Kind regards
    If the file exists and contains valid XML, and you've not altered the script, it should work. To debug that, you should first check that $items is an array prior to the foreach call:

    PHP Code:
    if (!is_array($items)) {
       echo 
    "Items is not an array!";
    }
    if (empty(
    $items)) {
       echo 
    "Items is empty!";

    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  10. #10
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,697
    Also, please take a look at "Debugging 101" in my signature. A lot of this is covered there IIRC.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Hi, sincere thanks for this.

    I'm going to go back to my script and work at it through tonight and see how I get on.

    I'll let you know.

    Kind regards

  12. #12
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Hi, yesterday, you both kindly provided a great deal of help to try and resolve the issue I had in opening saved images.

    After speaking to you, I did go back to my script and work through it line by line.

    I've made some changes to the script, which can be seen in it's entirety below:

    Code:
    <!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 
    
      $galleryPath = 'UploadedFiles/';
      //let's DEBUG the above assignment 
      if (!is_dir($galleryPath)) { die("No folder exists at $galleryPath!"); } 
    
      $absGalleryPath = realpath($galleryPath) . '/' .  $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/'; 
      
      //let's DEBUG this one too 
      if (!is_dir($absGalleryPath)) { die("No folder exists at $absGalleryPath!"); } 
      //if (!defined('DIRECTORY_SEPARATOR')) { echo "It's not defined!"; } 
    
      //if (!defined('DIRECTORY_SEPARATOR')) { echo "It's not defined!"; } 
      $descriptions = new DOMDocument('1.0'); 
    
      // DEBUG: let's check for the XML while we're at it 
      if (!file_exists($galleryPath . 'files.xml')) { die("No XML found at $absGalleryPath"."files.xml"); } 
      $descriptions->load($absGalleryPath . 'files.xml');
     
      $items = array();
      
      if (!is_array($items)) { 
       echo "Items is not an array!"; 
    } 
    if (empty($items)) { 
       echo "Items is empty!"; 
    } 
    
      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>
    When I run the script with this line included:

    Code:
     if (!file_exists($galleryPath . 'files.xml')) { die("No XML found at $absGalleryPath"."files.xml"); }
    I recieve the following error:

    No XML found at /homepages/2/d333603417/htdocs/development/UploadedFiles/IRHM73/1/files.xml
    The puzzling thing about this is, that the physical filepath is correct, and the 'files.xml' is physically at this location.

    However if I cancel out the error handling line, my page renders showing the correct folders, but when I click on the folder, I receive the following error:

    Code:
    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 28
    Items is empty!
    Warning: Invalid argument supplied for foreach() in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 71
    Line 28 is this line:
    Code:
    $descriptions->load($absGalleryPath . 'files.xml');
    and line 71 is this line:
    Code:
    foreach ($items as $key => $value) {
    I really appreciate all that you did yesterday and that you are both no doubt busy, but I just wondered if you perhaps have any other ideas that may help to resolve the problem because I admit to not being at all sure about what to do next.

    Kind regards and thanks

  13. #13
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    your errors are showing (still) that the xml isn't being loaded, which is why $items is empty.

    Have you checked the file/folder permissions (make sure php is allowed access)?

  14. #14
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Hi, many thanks for your reply.

    I've checked the access and that appears to be ok. I'll do some more digging around to see what I can find. I may have missed something when I was looking through the script earlier.

    Kind regards

  15. #15
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Hi, after going back through the code, I made some minor changes and my complete script is as follows:

    Code:
    <?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) . DIRECTORY_SEPARATOR; 
      
      //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>
    Unfortunately though, I'm still faced with this error message when I open the page:
    No XML found at /homepages/2/d333603417/htdocs/development/UploadedFiles/files.xml
    However, if I change this line:
    Code:
    $galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/';
    and delete the 'Session' variables and add the actual values, so the line looks like this:
    Code:
    $galleryPath = 'UploadedFiles/' . 'IRHM73' . '/' . '1' . '/';
    I have to say this has me totally baffled. I've double checked that the variables are being pulled through, and they are, I'm just at a loss as to what the solution may be.

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

    Kind regards and thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •