0
votes

The code I have should output a jpg from a list of files in a directory however it is not. I have trawled this site and tried different methods but not helped. I am a relative beginner at php so looking for any help at all.

I have tried using img src in the php code but I am trying to get the image to display within a Wordpress post so I cannot echo the img src within the script. I have tried file_get_contents and read file as well but it may be my lack of knowledge holding me back.

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

$arrnum = $GLOBALS[imagenum] - 1;
$dirname = PATH_IMAGES . $GLOBALS[imagepath]."/";
$images = scandir($dirname);
rsort($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
    if(!in_array($curimg, $ignore)) {
        header('Content-type: image/jpeg');
        file_get_contents('$dirname$images[$arrnum]');

        }
    }                 
?>
1
Assuming you're sending the correct image-type(are you sure they are all jpeg images) with the header, you can use file_get_contents() to render an image, but you need to echo the image...lovelace
when I use the following line: echo file_get_contents($dirname.$images[$arrnum]); I get the same issue as with readfile - it puts the blue square on the page and if I load up the php directly I get a lot of garbled letters etcDanny Waring
Then it sounds like they are not jpg files, have you tried var dump $images just to see what files it is pulling in?Matthew Page
The var dump of $images is string(45) "192.168.1.108_01_20181226154000368_TIMING.jpg" string(45) "192.168.1.108_01_20181226154000368_TIMING.jpg" string(45) "192.168.1.108_01_20181226154000368_TIMING.jpg" string(45)Danny Waring
just noticed, you seem to have forgotten a concatenation . between $dirname and $images[$arrnum] - use: file_get_contents($dirname . $images[$arrnum]);lovelace

1 Answers

0
votes

Have you tried readfile(...); should read and output the file. In your example you are not outputting the image data

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