random image php code

 random image php code

this code needs to be in test folder and outside of test folder there should be images folder where images should be placed

<?php

ini_set('display_errors', 1);

ini_set('display_startup_errors', 1);

error_reporting(E_ALL);


function RandImg($dir)

{

$images = glob($dir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)];

return $randomImage;

}


$the_image = RandImg('../images/');


//echo $the_image;


$file_out = $the_image; // The image to return


if (file_exists($file_out)) {


   $image_info = getimagesize($file_out);


   //Set the content-type header as appropriate

   header('Content-Type: ' . $image_info['mime']);


   //Set the content-length header

   header('Content-Length: ' . filesize($file_out));


   //Write the image bytes to the client

   readfile($file_out);

}

else { // Image file not found


    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");


}

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation