0

So I created an image.php file with this code and put it in my public_html folder:

<?php
header('Content-Type: image/jpeg');
readfile('home/folder/my image.jpg');
?>

and then I put this code in an html page in my public_html folder:

<img src="/image.php">

which perfectly displayed the "my image.jpg" file in my browser when I loaded the page. So this tells me that I do have access to the folder above my public_html folder (this is a dedicated server) and it tells me that I have no issues rendering a jpg file with spaces in the file name.

So now I'm trying to pull random images from the same folder, but can't seem to figure it out. Here is what I am doing that is NOT working.

I've created an image.php file with this code and put in my public_html folder:

<?php

//Define Function to Select Random Image
function random_pic($dir = '/home/folder')
{
$files = glob($dir . '/*.jpg');
$file = array_rand($files);
return $files[$file];
}

//Select Random Image Path
$randPic = random_pic('/home/folder');

//Output Random Image When image.php is called from html image requests
header('Content-Type: image/jpeg');
readfile($randPic);
?>

and then I put this code in an html page in my public_html folder:

<img src="/image.php">

Now when I load this html page with this img tag, I get nothing. No error, no image, nothing but white space. I appreciate any insight anyone can provide.

Bogarto
  • 37
  • 5
  • 1
    debugging steps: check that `glob()` is actually finding antyhing (e.g. `var_dump($files)`). Check that `random_pic()` is actually returning a path (e.g. `var_dump($randPic)`), then check that readfile isn't returning a boolean false (which you can't really var_dump, because the browser will try to read the dump text as image data). – Marc B Jul 19 '13 at 20:57
  • do a file_exists($randPic); and echo $randPic... see if it makes sense. – Orangepill Jul 19 '13 at 21:00
  • In addition to what Marc said, I'd also consider adding a "no-cache" flag to the header so they don't get the same image each time. Alternately, instead of using `readfile` you could use a `302 Temporary Redirect` and point directly to the image file. – Mr. Llama Jul 19 '13 at 21:02

2 Answers2

0

Why not use an absolute path instead of:

header('Content-Type: image/jpeg');
readfile('home/folder/my image.jpg');

like this:

// notice the absolute path prefix
if(!is_file($image_path = '/home/folder/my image.jpg')){
    header('Content-Type: text/plain', 404);
    echo 'Image file not found!';
}

// We got a file, keep going...
header('Content-Type: image/jpeg');
header('Content-Length: '.filesize($image_path));
readfile($image_path);

die; // done here

Or use the path relative to current directory of .php script with __DIR__.'/path/to/image.jpg'.

CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • I just want to make sure I understand. So with regard to pulliing a random image, instead of using this code: header('Content-Type: image/jpeg'); readfile($randPic); I should be using something else? It looks like you referenced my output code from my working solution and not from my non-working random image solution. My apologies if I misunderstood. – Bogarto Jul 19 '13 at 21:42
  • @Bogarto You code is apparently good. You need to `var_dump($files)` inside the `random_pic` function and make sure you see anything listed by the `glob` result. That's why I thought the relative path may be an issue... – CodeAngry Jul 19 '13 at 21:55
0

You have to put safe_mode = off or configure open_basedir to get access to the images directory.

Zeque
  • 1