0

I have this script in a file (called random.php) which displays a random image from the folder where it resides:

<?php 
$pics = glob('*.jpg', GLOB_NOSORT); 
$pic = $pics[array_rand($pics)]; 
header("Content-type: image/jpeg"); 
header("Content-Disposition: filename=\"" . basename($pic) . "\""); 
readfile($pic); 
?>

I am calling it as follows:

<img class="random" src="http://www.example.com/random.php" />

It works properly.

I want instead to make it to display random pictures by pulling their urls from a text file full with lines, each line being an image url. How to do this?

Final update: This is what worked for me.

<?php 
$file = 'random.txt';

if (!is_readable($file)) {
    exit('File list does not exist, or is not readable by the webserver.');
}

$pics = file('random.txt', FILE_SKIP_EMPTY_LINES); 

$pic = $pics[array_rand($pics)]; 

if (!getimagesize($pic)) {
    exit('Image does not exist, or is not readable by the webserver.');
}

/// content type
header('Content-Type: image/jpeg');
// prevent caching (so its random)
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
//


readfile($pic); 
?>

Is the script kindly compiled below by Lawrence Cherone but commenting out the last header tag.

1 Answer 1

1

Switch out glob('*.jpg', GLOB_NOSORT); for file().

$pics = file('/path/to/file.txt', FILE_SKIP_EMPTY_LINES);

Try this, with added error checking:

<?php 
$file = 'random.txt';

if (!is_readable($file)) {
    exit('File list does not exist, or is not readable by the webserver.');
}

$pics = file('random.txt', FILE_SKIP_EMPTY_LINES); 

$pic = $pics[array_rand($pics)]; 

if (!getimagesize($pic)) {
    exit('Image does not exist, or is not readable by the webserver.');
}

// content type
header('Content-Type: image/jpeg');
// prevent caching (so its random)
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
//
header('Content-Disposition: filename="'.basename($pic).'"');

readfile($pic); 
?>
Sign up to request clarification or add additional context in comments.

6 Comments

don't put code in comments, instead update your question with your exact code. your need to debug it, as whats happening is your outputting whitespace or file() is throwing an error (maybe permissions or not found) and when header is set it throws an error.
As you suggested I updated my original post with the code which needs debugging. Could you kindly write the complete script as it is supposed to be? I only know to copy and paste scripts, thank you for your understanding.
@ Lawrence The content of random.txt is just two lines, each being an URL of an image hosted on google
@Konstantinos See updated code, i've tested it and it works fine.
@Konstantinos added some additional headers to stop browser caching.
|

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.