Created
March 12, 2018 16:09
-
-
Save jhammann/2a0d4d66842daf19689f66b4e6752e70 to your computer and use it in GitHub Desktop.
Serve a random image (JPG) from a specific directory which contains the images.
This file contains 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 | |
// Scan the images directory, remove the folder indicators and reset the index. | |
$files = array_values(array_diff(scandir('./images'), array('.', '..'))); | |
// Get a random file from the array. | |
$int = rand(0, (count($files) - 1)); | |
$file = $files[$int]; | |
// Serve the file as JPG content. | |
header('Content-Type: image/jpeg'); | |
readfile('./images/' . $file); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment