Jump to content

Unable To Load Image Data With Dynamic Folders


hobbiton73

Recommended Posts

I wonder whether someone may be able to help me please.

 

I've put together the following script which saves user defined images to server folders using the Aurigma Image Uploader software.

 

<?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"]; 
  $usernamefolder = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['username']); 
  $locationfolder = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['locationid']); 
  $dirName = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['folder']); 

  $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $usernamefolder . DIRECTORY_SEPARATOR . $locationfolder . 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(); 
?>

 

This code saves the files in the following structure:

 

UploadedFiles (Pre-existing folder)

  • 'username' (Subfolder created dynamically on image upload if it doesn't already exist. This folder contains the 'location' folder)
  • 'location' (Subfolder created dynamically on image upload if it doesn't already exist. This contains original image, 'files.xml' and 'Thumbnails' folder)
  • 'Thumbnails' (Subfolder created dynamically on image upload if it doesn't already exist. This contains thumbnail of original image)

 

I'm now trying to put together a script which creates an image gallery, with those images pertinent to the current user and location. The code below is the piece of my script which deals with the loading of the images.

 

<?php 
  $galleryPath = 'UploadedFiles/'; 
    
  $thumbnailsPath = $galleryPath . 'Thumbnails/'; 
    
  $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 
    
  $descriptions = new DOMDocument('1.0'); 
  $descriptions->load($absGalleryPath . 'files.xml'); 
?>

 

The initial problem I had, was resolved from guidance I received on this site, and that was to find the correct 'realpath' of the 'Thumbnails' folder and 'files.xml'

These are as follows:

/homepages/2/d333603417/htdocs/development/UploadedFiles/IRHM73/1
  and
/homepages/2/d333603417/htdocs/development/UploadedFiles/IRHM73/1/Thumbnails
with 'IRHM73' being the 'username' value and '1' the 'location' value.

 

The problem I now have lies within this line of code:

 

`$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR;`

 

Because the 'username' and 'location' are dynamic and hence contain different values for each user and location, I've found it impossible to find a way of creating the correct file path to open the 'Thumbnails' folder and 'files.xml'.

 

I had tried this:

 

<?php 
   
  $galleryPath = 'UploadedFiles/'; 
    
  $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $usernamefolder . DIRECTORY_SEPARATOR . $locationfolder . DIRECTORY_SEPARATOR; 
  $absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR; 
    
  $descriptions = new DOMDocument('1.0'); 
  $descriptions->load($absGalleryPath . 'files.xml'); 
?>

 

But I receive the following error: 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/gallery.php on line 13 and line 13 is this: 

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

 

I really am stumped with what to do next to get this to work. I appreciate that this is a fairly lengthy post, but thought it would be better to post all the information.

 

I just wondered whether someone could perhaps take a look at this and let me know where I'm going wrong and how I can get it to work.

 

Many thanks and regards

Link to comment
Share on other sites

$galleryPath = 'UploadedFiles/';
$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $usernamefolder . DIRECTORY_SEPARATOR . $locationfolder . DIRECTORY_SEPARATOR;
$descriptions->load($absGalleryPath . 'files.xml'); 

realpath/UploadedFiles/usernamefolder/locationfolder/files.xml

^ something like that should the path be in the error.

 

Let us say this is the realpath to the folder your script is in:

/homepages/2/d333603417/htdocs/development/

Then this is all there is left of the path in the error:

UploadedFiles/'files.xml'

And it should be something like this:

UploadedFiles/usernamefolder/locationfolder/files.xml

Of course usernamefolder and locationfodler replaced by some other strings.

 

That doesn't really look like the same path?

Could you try to echo it right before loading it?

Are you sure this is the updated script you just ran and got that error with?

Link to comment
Share on other sites

Hi, many thanks for helping me out, again  :)

 

I've used the line of code you suggested, so that the script looks like:

 

<?php 

$galleryPath = 'UploadedFiles/';$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $usernamefolder . DIRECTORY_SEPARATOR . $locationfolder . DIRECTORY_SEPARATOR;$descriptions->load($absGalleryPath . 'files.xml'); 

$descriptions = new DOMDocument('1.0'); 
$descriptions->load($absGalleryPath . 'files.xml'); 
?>

 

Unfortunately, when I run this, I receive the following error:

 

Fatal error: Call to a member function load() on a non-object in /homepages/2/d333603417/htdocs/development/gallery.php on line 6

 

Line 6 is this line:

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

 

I think that problem isn't necessarily the file path, but I'm unsure about what the 'username' and 'location' folders should be called in the file path because they are dynmaic values, if that makes sense?

 

Kind regards

Link to comment
Share on other sites

I didn't say you should do that code, I said that's your code, and I tried to figure out why it didn't look like the one in the error.

 

You said this code:

<?php 
   
  $galleryPath = 'UploadedFiles/'; 
    
  $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $usernamefolder . DIRECTORY_SEPARATOR . $locationfolder . DIRECTORY_SEPARATOR; 
  $absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR; 
    
  $descriptions = new DOMDocument('1.0'); 
  $descriptions->load($absGalleryPath . 'files.xml'); 
?>

 

Resulted in this error:

But I receive the following error: 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/gallery.php on line 13 and line 13 is this: 

etc

 

My point with the last post was to prove that it didn't exactly look like you had upgraded the script, as I couldn't figure out how your script could produce the path in the error.

Link to comment
Share on other sites

Hi, my sincere apologies for confusing things.

 

I can confirm that this:

 

<?php      

$galleryPath = 'UploadedFiles/';  
     
$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $usernamefolder . DIRECTORY_SEPARATOR . $locationfolder . DIRECTORY_SEPARATOR;   
$absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR;    
   
$descriptions = new DOMDocument('1.0');   $descriptions->load($absGalleryPath . 'files.xml'); 
?>

 

Creates this error when I run the 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/gallery.php on line 7

Kind regards

Link to comment
Share on other sites

Before this line:

$descriptions = new DOMDocument('1.0');   $descriptions->load($absGalleryPath . 'files.xml');

Put this:

echo '$galleryPath = ' . $galleryPath . '<br/>';
echo 'realpath($galleryPath) = ' . realpath($galleryPath) . '<br/>';
echo '$usernamefolder = ' . $usernamefolder . '<br/>';
echo '$locationfolder = ' . $locationfolder . '<br/>';
echo '$absGalleryPath = ' . $absGalleryPath . '<br/>';

 

And tell us what it now says, along with the error that usually appear.

Link to comment
Share on other sites

Hi, many thanks for this.

 

Other than changing 'usernamefolder' to 'username' and 'locationfolder' to 'location I've added your code, so the script is:

 

<?php      
echo '$galleryPath = ' . $galleryPath . '<br/>';
echo 'realpath($galleryPath) = ' . realpath($galleryPath) . '<br/>';
echo '$username = ' . $username . '<br/>';
echo '$location = ' . $location . '<br/>';
echo '$absGalleryPath = ' . $absGalleryPath . '<br/>';

$descriptions = new DOMDocument('1.0');   
$descriptions->load($absGalleryPath . 'files.xml'); 
?>

 

Upon running this I receive the following:

 

$galleryPath =

realpath($galleryPath) = /homepages/2/d333603417/htdocs/development

$username =

$location =

$absGalleryPath =

 

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "/homepages/2/d333603417/htdocs/development/files.xml" in /homepages/2/d333603417/htdocs/development/gallery.php on line 11

 

Kind regards

Link to comment
Share on other sites

Hi, apologies, I had missed off the '$galleryPath' value off my last post.

 

$galleryPath = UploadedFiles/

realpath($galleryPath) = /homepages/2/d333603417/htdocs/development

$username =

$location =

$absGalleryPath =

 

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "/homepages/2/d333603417/htdocs/development/files.xml" in /homepages/2/d333603417/htdocs/development/gallery.php on line 11

 

Regards

Link to comment
Share on other sites

Let us say the problem is here somewhere:

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

At some point here $usernamefolder and $locationfolder becomes empty strings.

You will have to print it all the way through, to see what makes them empty strings, maybe they get it from $uploadedFile object.

Link to comment
Share on other sites

Hi, i just wanted to give you an update re. the problem I had with loading the images.

 

I've now corrected this by inclusing 'session variables' so the working code is:

 

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

Kind regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.