0

I am trying to show an image on a Wordpress post from a folder on the server. The images in the folder are put into an array using scandal and the . and … are ignored. The folder that the image comes from is selected using GET and is put into the url at the end of the address along with the image number. This is so the latest image (which is timestamped on the filename can be chosen using the array number.

Loading up the URL directly using "http://www.weburl.com/wp-content/.../getlatestimage.php?image=pap&num=1 will load up the image directly however if I try and put it in as img src or via an image block will not load the photo into the page. I believe that this is because the code echos an in the php but I do not know how to load it into a constructed webpage any other way.

Here is the code I have written:

<?php
$imagepath = htmlspecialchars($_GET["image"]);
$imagenum = htmlspecialchars($_GET["num"]);

define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] );

If(LOCALHOST){
    define('PATH_IMAGES', 'this_path');
}else{
    define('PATH_IMAGES', '../../../Images/');
}
?>


<ul>
<?php
    $arrnum = $GLOBALS[imagenum] - 1;
    echo $arrnum;
    $dirname = PATH_IMAGES . $GLOBALS[imagepath]."/";
    $images = scandir($dirname);
    rsort($images);
    $ignore = Array(".", "..");
    foreach($images as $curimg){
        if(!in_array($curimg, $ignore)) {
            //echo "<li><a href='".$dirname.$curimg."'></li>";
            header('Content-type: image/jpeg');
            //echo file_get_contents($dirname$images[$arrnum]);
            echo "<img src='$dirname$images[$arrnum]'width='1600'";

        }
    }                 
?>

Any and all help will be really appreciated.

  • Just looking around to see what the best way to go about this is. According to [this](https://stackoverflow.com/questions/17122218/get-all-the-images-from-a-folder-in-php) `opendir` might be a better way to go about it. – Tom Dickson Jan 17 '19 at 22:51
  • So what does your HTML look like? And why are you using `header()` like that? – miken32 Jan 17 '19 at 22:57
  • You echo before headers. That should give you an error. If you do not have errors you need to enable error display in PHP – Dharman Jan 17 '19 at 23:21
  • I am still a relative beginner so I am look for advice from yourselves on how to get this working – Danny Waring Jan 19 '19 at 18:06

0 Answers0