Skip to content

Instantly share code, notes, and snippets.

@jhammann
Created March 12, 2018 16:09
Show Gist options
  • Save jhammann/2a0d4d66842daf19689f66b4e6752e70 to your computer and use it in GitHub Desktop.
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.
<?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