0

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

        }
    }                 
?>
14
  • 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... Commented Jan 19, 2019 at 21:22
  • 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 etc Commented Jan 19, 2019 at 21:29
  • Then it sounds like they are not jpg files, have you tried var dump $images just to see what files it is pulling in? Commented Jan 19, 2019 at 21:44
  • 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) Commented Jan 19, 2019 at 21:53
  • just noticed, you seem to have forgotten a concatenation . between $dirname and $images[$arrnum] - use: file_get_contents($dirname . $images[$arrnum]); Commented Jan 19, 2019 at 22:01

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

I have replaced file_get_contents with readfile - It still just shows the blue square which putting img src on the WP Post. When I load up the php directly using the correct variables it just shows a full screen of letters numbers and question marks in diamonds
If you're getting a screen full of letters and numbers then it sounds like a problem with the content-type . You could try Content-Type (capital T) I seem to remember something about that...
I have changed the t to a T and it has made no difference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.