Skip to content

Instantly share code, notes, and snippets.

@rtgibbons
Created June 3, 2011 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtgibbons/1005740 to your computer and use it in GitHub Desktop.
Save rtgibbons/1005740 to your computer and use it in GitHub Desktop.
download random image
<?php
$files = array();
$path = 'i/s/';
if ( $dir = opendir( $path ) ) {
while ( false !== ( $file = readdir( $dir ) ) ){
switch( mime_content_type( $path.$file ) ) {
case 'image/jpeg':
case 'image/jpg':
case 'image/png':
case 'image/gif':
$files[] = $path . $file;
break;
}
}
}
$key = ( array_rand( $files ) );
header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type($files[$key]));
header('Content-Disposition: attachment; filename='.basename($files[$key]));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($files[$key]));
ob_clean();
flush();
readfile($files[$key]);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment