Image paths using $storagelocation

Having issue using $storagelocation. for image paths.

If I use:

$imgpath = $storagelocation.'math_001/images/product.jpg';

Then echo:

background-image: url(<?php echo $imgpath; ?>);

This is the results:

background-image: url(C:\Users\oleteacher\AppData\Local\math2020\{C5B591E0-B0C2-4801-A38F-23936D091CB3}\math_001/images/product.jpg);

I need to work out of the users appdata folder and display images. Have even tried placing file:/// in front of C: and no luck.

The image not displaying within my compiled software. What am I doing wrong?

As I recall, for browser security reasons it’s not possible to display regular image files direct from folders anywhere on a users PC, I believe they have to be compiled with the actual exe. However, I did find a workaround…

First create a php file that reads image files and outputs them as a stream using readfile - somethnig like this:

<?php

if (isset($_GET['fname'])){$fname=$_GET['fname'];}else{exit;}
if (isset($_GET['fext'])){$fext=$_GET['fext'];}

$storagelocation = exo_getglobalvariable('HEPubStorageLocation', '');   
$file=$storagelocation.'math_001/images/'.$fname.'.'.$fext;

 if (file_exists($file)) {
     header('Content-Description: File Transfer');
     header('Content-Type: application/octet-stream');
     header('Content-Disposition: attachment; filename="'.basename($file).'"');
     header('Cache-Control: public, max-age=31536000');
     header('Pragma: public');
     header('Content-Length: '.filesize($file));
     header('Connection: Close');
     readfile($file);
     exit;
    }

Save the above as image.php

Then in the HTML on the page where you put the img tag, set the src to the image.php file and send parameters (file name & extension) so the script knows which image to output, somthing like this:

<img src="image.php?fname=product&fext=jpg" />

I’ve not tested the technique with CSS background images, but it certainly works fine for regular img tags, so fingers crossed!

1 Like

Thank you @kim ! That is pretty cool idea for image display:)

I gave it a quick run and did not get it to work. At first thought it was because of missing ’ after fext (in second line). But cannot get image to display. Seems to be looking at the “virtual” path http://heserver/ when using image.php?fname=product&amp;fext=jpg

I also had disabled WebSecurity and was thinking the file:/// path would work, but you are right browser security not allowing.

oops, sorry, fixed it now!

That was my fault, I rushed it late last night, there was a skoolboy error in the first line too, so also fixed that - try it again now :wink:

No guarantees it’ll work to pull in background images in CSS though - but it might.

Probably worth noting, while it should work when the exe has been compiled, there can be issues if the page calls too many images at once, say for example you have 100 small images on a page all attempting to display upon page load, exeoutput occasionally throws a system error - can’t remember which error it was, but it was enough for me to have to limit the amount of images displayed on a page to ensure stability.

You can experiment with the different headers as I don’t think all are required for the technique to work, those you see there are just the result of me meddling around attempting to fix the above issue that I had to give up on in the end - but it’s the exact code I currently use, so it should work fine for now - just be mindful of trying to call too many images on a page at once.

Maybe the dev might have an idea about that problem? (I did screenshot the error somewhere, so if I can find it I’ll post it - I’d planned to do that ages ago, just been flat out with other work of late)

EDIT: It was a list index out of bounds error, occasionally followed by a php-cgi.exe has stopped working…:
EXO-DLL-EEROR-(list-index-o

exe_has_stopped_working

As is often the case with errors, it was pretty random, sometimes I’d not see one for a while, then have several in a few hours use, but eventually tracked it down to the images. Once the images had been cached in the browser it was not a problem regardless of how many were on the page, subsequent page refreshes with the same images using the above technique worked fine - but loading a page that called 100 brand new images via php would occasionally cause the crash. The issue only started after upgrading to exeout 2018 as I recall - and still seems to affect the current version.

I would hug you if I could @kim ! It is now working and I should have caught the filename / fname issue. Was also late for me last night. Amazing what little sleep does to the old eyes.

Only have handful of images that need to be placed on each page, so hopefully the error will stay at bay.

Cannot thank you enough for your advice and help.

@gdgsupport Any idea what your cause the above error shown by @kim

Cheers!

We’ll investigate this “list index out of bounds” error

@oldteacher Excellent, glad you got it working!

@gdgsupport Thanks!

This is a wonderful solution. But, if you do not know what the image name will be, will not work.

Anyone know if possible to modify the above code to work, if you do not know the image name? Users upload images which are stored in their appdata folder.

With current configuration you must know the image name. Like to be able to upload an image and have it displayed from the appdata folder on page within software.

I use scandir to grab the file names in the folder first, then dynamically generate the IMG tags based on the results.

This link should point you in the right direction:

  • in addition, if you only want to grab the most recent file, you could then cycle through the files found, and use the filemtime function to grab the last modified time and only generate an img tag for the most recent.

http://php.net/manual/en/function.filemtime.php

1 Like

Thank you @kim! Good ole Stackoverflow, got ta love that site. Funny, I came across that info month or so ago but could not grasp how to get the dir info and then apply to show the image(s).

I normally go to my husband for advanced info but he is off on Church mission in Peru. Will have to put my own thinking cap on:)

Thanks again for the tip.

Been giving this some thought… Just finished a Chrome Extension for a client which has a dashboard and there are image in the dashboard. Chrome Extensions install in the users AppData folder under local/google/chrome/user data/default/extensions. These images are shown in browser since extension has settings? If they show there, why not within the software by EXEOut?

Anyway, I have spent days trying to figure out how to use scandir to dynamically generate IMG tags, no luck and information is all over the board.

@gdgsupport Have you ever found a way to load images placed in user AppData folder within EXEOut apps? Surely there must be a way and hope you can take time to explain this.

I thank you @kim for all the tips!

kim has the sole solution that will work. You have to scan for images and use a loop to generate the img tags: <img src="image.php?fname=XXX&fext=jpg" />.

You would get something like (not tested):

Thanks @gdgsupport for code. I have used similar and all that is returned are broken images to exeout browser on echo.

Using your suggested code results in same behavior, broken images. Using your code I see <img src="image.php?fname=10.jpg&fext=jpg"> (image 10.jpg does exist) but images are always broken.

Will continue to troubleshoot on my end. Must be something I am doing wrong:)

Sorry Susan, all been a bit hectic at this end, looking after a very poorly Dad at the mo… barely had any time to think let alone focus on code lately… grrr!

Try this:

$storagelocation = exo_getglobalvariable('HEPubStorageLocation', '');
$folder=$storagelocation.'math_001/images/';
$allFiles=scandir($folder);
$files= array_diff($allFiles, array('.','..'));
foreach($files as $file)
       {
        $f=explode(".",$file);$name=$f[0];$ext=$f[1];
        if ($ext=="jpg" || $ext=="png" || $ext=="jpeg" || $ext=="gif")
           {
            echo '<img src="image.php?fname='.$name.'&fext='.$ext.'">';
           }
       }

Untested, but should work :wink:

1 Like

@kim, very sorry to hear about your Dad. Been there done that with my Mom and husbands Dad. It can be trying but we only have one Dad:) Wishing him the best.

You are a hero! It does work now. Can see all the images appear in exeout browser.

Now I have to figure out how to best echo the image in my HTML img src tag, combining your above code and the code from image.php.

Thank you so much for taking time to help. With the “PHP husband” away, I am struggling:) Have worked with Microsoft Visual Studio for some years but PHP very little.

1 Like

This has certainly been an eye opener, challenge and best of all, great learning experience!

Something that has me stumped is how to use in association with image uploading. If my script uploads an image to the storage folder, at a loss how to use the image.php method to display back on page.

Is it even feasible? Any pointers certainly appreciated if possible to use uploading and image.php

Thanks!

You need to get the last modified time of the files in the folder, and and create an image tag to display just the most recent one. So essentially what you’d do is first create a loop that scans the files in the folder like the one above, and on each cycle grab the filename and store it in an array keyed with the last mod time of the file that can be grabbed using the filemtime function I mentioned in an earlier post.

Then sort the array by its key (file last mod time - largest number will be the most recently uploaded file so it will end up first in the array after sorting), and create an image tag to display just the first element in the array.

Let me know how you get on!

[EDIT] - I thought the SCANDIR_SORT_DESCENDING flag could be a shortcut to do what you need, but turns out it sorts the files alphabetically, and you need to sort by by last mod time, so unfortunately you have to do it the longer way round! - this should point you in the fight direction, but best to use filemtime rather than filectime: https://stackoverflow.com/questions/1491020/get-the-latest-file-addition-in-a-directory

Thank you @kim for the explanation! This furthers my belief I need to get a study course in PHP. Got to quit depending on the husband, lol (out of country anyway).

I am looking at the stackoverflow link and will experiment with it and your tips.

In a simple test on local server using:

$path = "images"; 

$latest_mtime = 0;
$latest_filename = '';    

$d = dir($path);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path}/{$entry}";
  // could do also other checks than just checking whether the entry is a file
  //if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
  if (is_file($filepath) && filemtime($filepath) > $latest_mtime) {
    //$latest_ctime = filectime($filepath);
    $latest_mtime = filemtime($filepath);
    $latest_filename = $entry;
  }
}

echo $latest_filename;

I do get the latest file uploaded, so gives me hope:)

Appreciate it much. If/when I get a working solution will certainly post here. Surely some other sole can use in future.