Last active
August 29, 2015 14:01
-
-
Save CoolOppo/c9b967f2a955ae07192e to your computer and use it in GitHub Desktop.
Displays a random GIF image at an unchanging direct URL/URI using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function random_pic($dir = '/var/www/images') { | |
$files = glob($dir . '/*.gif'); | |
if (!$files) return false; | |
$file = array_rand($files); | |
return $files[$file]; | |
} | |
function outputImage( $filename ) { | |
header("Content-Type: image/gif"); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
header('Content-Length: ' . filesize($filename )); | |
ob_clean(); | |
flush(); | |
readfile($filename ); | |
exit; | |
} | |
// Get a filename | |
$filename = random_pic(); | |
// Check that a file was found | |
if (!$filename) { | |
header('HTTP/1.1 404 Not Found'); | |
die(); | |
} | |
// Output the image | |
outputImage( $filename ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment