Image paths using $storagelocation

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