Jump to content

Session Variable


hobbiton73

Recommended Posts

I wonder whether someone can help me please.

 

I'm using the script below to create a page whereby users are presented with a list of image folders they have created. Clicking on any of the folders allows the user to drill down and view the individual images.

 

<?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'] . '/';
  
  $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/> → <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>

 

I can create the list of folders, but when I click on any of these, instead of being able to view the 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 16

 

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

 

Line 16 is this line

$descriptions->load($absGalleryPath . 'files.xml'); 

and line 52 is this

foreach ($items as $key => $value){

 

However, if I change this line

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

to

$galleryPath = 'UploadedFiles/' . 'IRHM73' . '/' . '1' . '/';

i.e. replacing the 'Session Variables' with the actual values, the page works.

 

I've been working on this for days now, and I just can't find the solution. I just wondered whether someoen could perhaps have a look at this and let me know where I'm going wrong.

 

Many thanks and regards

Link to comment
Share on other sites

After doing some more testing, I've found that the problem is around passing the 'Session Variables' to the 'foreach' array.

 

My amended code below, now shows correct folders, and allows the user to select any of these. However, unfortunately, the individual images fail to load. It is this aspect of the script which the 'foreach' array is used.

 

<?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/> → <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>

 

I'm a relative beginner in PHP, and having researched 'Session Variables' and 'arrays', I have to say I've struggled to understand perhaps what most would call 'the basics'.

 

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

 

Many thanks and kind regards

 

Link to comment
Share on other sites

individual images fail to load

 

^^^ EXACTLY what does that mean and at EXACTLY what point does the problem occur at? Are the links to the images present, correctly formatted, and have the correct path and image name? What does an actual link to an image look like? When you click on a link, is the image broken or is there some other error occurring?

 

The code you posted doesn't have a session_start() statement in it so setting or referencing $_SESSION variable will only work on that page. You are also unconditionally assigning some post data to session variables, so any time that page gets requested via a get requested (i.e. you first browse to the page), the post variables will be empty and the session variables will be assigned empty values too.

 

Also, you would need to post a working sample files.xml for any one here to directly help. We're not going to sift through the logic tying to figure out what data the code is using as an input, which itself might be where the problem lies. Supply enough information so that someone could actually see what the code is supposed to be doing without spending more time to reverse engineer it and synthesize test data than what it would probably take to solve the problem.

Link to comment
Share on other sites

I'll make a separate reply for this.

 

Where exactly are you trying to set the two session variables from? A log in script? In which case you expect them to already have values in them when you visit that page and do you also want to limit access to that page only if a person is logged in?

 

Your current code is setting them via hard code values from hidden fields in your form, so they won't be set until after the form has been submitted the first time. Did you do it this way to compensate for some other symptom associated with the session variables?

Link to comment
Share on other sites

Hi, many thanks for taking the time to reply to my post, and my sincere apologies for how lacking it was in detail. I hope that I can rectify this with the information below.

 

Firstly, I'd like to say that I am relatively new to PHP and this is my first attempt using 'Session Variables'. I've put together these pages from tutorials I've been working through, but I'm certainly willing to learn from a more seasoned programmer, such as yourself, better ways of doing this.

 

Please find this link http://www.mapmyfinds.co.uk/development/frontpage.php to my test pages which will hopefully help aid my explanantion of the problem.

 

The first page shows hardcoded values in the 'username'and 'locationid' fields. This page was purely put together so that I can pass the variables between pages. It will be replaced by a webpage once I've sorted out the images. The pages will only show, and allow, the user to add and view the images pertinent to them and to the 'location' record that they are adding the images to. The user will only be able to access these pages once they are logged in. Access control has already been built into the website.

 

If you click on the 'Submit Query' button, this will take you to the page that allows the user to add images to the 'location' record. Please note, I've taken the upload functionality for the purposes of this testing.

 

Click the 'View Uploaded Uploaded Images In Folder Structure' link at the top of the page. This will take you to the page I'm having difficulty with.

 

You will see that I have set up two folders, 'Test 1' and 'Test 2'. If you click on either of these, the error message will be created.

 

As requested, please find below the 'files.xml' file

 

  <?xml version="1.0" encoding="utf-8" ?>

- <files>

  <file name="Test 1/_47061196_greatbritainjpg.jpg" source="_47061196_greatbritainjpg.jpg" size="227505" originalname="_47061196_greatbritainjpg.jpg" thumbnail="_47061196_greatbritainjpg.jpg" description="No description provided" username="IRHM73" locationid="1" folder="Test_1" />

  <file name="Test 2/A-deer-in-Knole-Park-Seve-005.jpg" source="A-deer-in-Knole-Park-Seve-005.jpg" size="89509" originalname="A-deer-in-Knole-Park-Seve-005.jpg" thumbnail="A-deer-in-Knole-Park-Seve-005.jpg" description="No description provided" username="IRHM73" locationid="1" folder="Test_2" />

  <file name="Test 2/article-0-07D01B74000005DC-138_468x617.jpg" source="article-0-07D01B74000005DC-138_468x617.jpg" size="143110" originalname="article-0-07D01B74000005DC-138_468x617.jpg" thumbnail="article-0-07D01B74000005DC-138_468x617.jpg" description="No description provided" username="IRHM73" locationid="1" folder="Test_2" />

  </files>

 

After I wrote my original post, I continued working on the 'imagefolders.php' file which for clarity and completeness is also shown below:

 

<?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/> → <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>

 

Using the the 'var_dump' functionality I can confirm that up to this point in my code:

$files = array();

the information is being passed through the '$items' array. From then on, the array values come back as 'NULL'.

 

Again, for completeness, should the need arise to look at this, I have added my 'upload.php' script below.

 

<?php
require_once 'Includes/gallery_helper.php'; 
require_once 'ImageUploaderPHP/UploadHandler.class.php'; 
$galleryPath = 'UploadedFiles/'; 

function onFileUploaded($uploadedFile) { 
  global $galleryPath; 

  $packageFields = $uploadedFile->getPackage()->getPackageFields(); 
  $username=$packageFields["username"]; 
  $locationid=$packageFields["locationid"]; 
  $username = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['username']); 
  $location = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['locationid']); 
  $dirName = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['folder']); 

  $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $username . DIRECTORY_SEPARATOR . $location . DIRECTORY_SEPARATOR; 
  $absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR; 

  if (!is_dir($absGalleryPath)) mkdir($absGalleryPath, 0777, true); 
  chmod($absGalleryPath, 0777); 
  if (!is_dir($absGalleryPath . $dirName)) mkdir($absGalleryPath . $dirName, 0777, true); 
  chmod($absGalleryPath . $dirName, 0777); 

  if ($uploadedFile->getPackage()->getPackageIndex() == 0 && $uploadedFile->getIndex() == 0) 
    initGallery($absGalleryPath, $absThumbnailsPath, FALSE); 

  $originalFileName = $uploadedFile->getSourceName(); 
  $files = $uploadedFile->getConvertedFiles(); 
  $sourceFileName = getSafeFileName($absGalleryPath, $originalFileName); 
  $sourceFile = $files[0]; 

  if ($sourceFile) $sourceFile->moveTo($absGalleryPath . $sourceFileName); 
  $thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName); 
  $thumbnailFile = $files[1]; 
  if ($thumbnailFile) $thumbnailFile->moveTo($absThumbnailsPath . $thumbnailFileName); 
  
  $descriptions = new DOMDocument('1.0', 'utf-8'); 
  $descriptions->load($absGalleryPath . 'files.xml'); 
  
  $xmlFile = $descriptions->createElement('file'); 
  // <-- please check the following line 
  $xmlFile->setAttribute('name', $_POST['folder'] . '/' . $originalFileName); 
  $xmlFile->setAttribute('source', $sourceFileName); 
  $xmlFile->setAttribute('size', $uploadedFile->getSourceSize()); 
  $xmlFile->setAttribute('originalname', $originalFileName); 
  $xmlFile->setAttribute('thumbnail', $thumbnailFileName); 
  $xmlFile->setAttribute('description', $uploadedFile->getDescription()); 
  $xmlFile->setAttribute('username', $username); 
  $xmlFile->setAttribute('locationid', $locationid); 
  $xmlFile->setAttribute('folder', $dirName); 


  $descriptions->documentElement->appendChild($xmlFile); 
  $descriptions->save($absGalleryPath . 'files.xml'); 
} 

$uh = new UploadHandler(); 
$uh->setFileUploadedCallback('onFileUploaded'); 
$uh->processRequest(); 
?>

 

I hope that this provides you with the information that you need and once again, many thanks for taking the time to help.

 

Kind regards

Link to comment
Share on other sites

The problem is how the $items array is being built and how you iterate over it later. $items is created as nested arrays, one level for each part of the exploded 'name' field. The code you are using later to iterate over the $items array was apparently written to work without a path as part of the name="..." attribute. You would need to write a recursive function to make your current code work.

 

However, considering that there is a folder="..." attribute (and a src attribute), I suspect that the name="..." attribute was intended to ONLY be a 'display' name for the image and the folder="..." attribute should be the path to the actual file.

 

To see why your code is not finding and setting the $files[] elements, add the following echo .....; statement right after your existing foreach(){ statement -

 

foreach ($items as $key => $value) {
echo '<pre>Value:',print_r($value,true),'</pre>';
...

}

Link to comment
Share on other sites

Hi, many thanks for this it's greatly appreciated.

 

I added the line you suggested, and this is the output I received:

 

greatbritainjpg.jpg] => Array

        (

            [/src/] => _47061196_greatbritainjpg.jpg

            [description] => No description provided

            => 227505

        )

 

To a beginner, this looks as though the information is being pulled through correctly, but I'm not sure how to take this further.

 

I must admit, it's been a bit of a headache using the Aurigma Image Uploader code, but, I couldn't find anything that had a similar look and functionality to the Aurigma package.

 

Kind regards

)

Value:Array

(

    [A-deer-in-Knole-Park-Seve-005.jpg] => Array

        (

            [/src/] => A-deer-in-Knole-Park-Seve-005.jpg

            [description] => No description provided

            => 89509

        )

 

    [article-0-07D01B74000005DC-138_468x617.jpg] => Array

        (

            [/src/] => article-0-07D01B74000005DC-138_468x617.jpg

            [description] => No description provided

            => 143110

 

To a beginner, this looks as though the information is being pulled through correctly. I have to confess though I'm not really sure how to take this further and rectify the problem.

 

I have to admit, using the Aurigma Image Uploader code has been a bit of a headache, but I couldn't find anything that gave a similar look and functionality of the Aurigma package.

 

Kind regards

Link to comment
Share on other sites

The line right after that - if (isset($value['/src/'])) { is trying to find first level array keys named '/src/' to distinguish between file entries and directories. You will note that the first level array keys at that point are the file names and that the '/src/' key is one more nested level inside each array.

 

As already stated, it appears that the the name="..." attribute is supposed to be just the name and that the folder="..." attribute is supposed to contain any path information. You need to remove the hacked code that adds the raw path value into the name="..." attribute. Then, when you iterate over the .xml data, you should read the folder="..." attribute and store that into the $items array.

Link to comment
Share on other sites

Archived

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.