Quick Search:
Oracle PL/SQL Code Library
Find Or Post Oracle Jobs
Oracle Discussion & Chat
 PHP Code: Random Image from Directory Jump to:  
Category: >> PHP Code >> Random Image from Directory Bookmark and Share

<< lastnext >>

Snippet Name: Random Image from Directory

Description: Spits out a random image path from a given directory.

Also see:
» Random banner picker
» Generate random string
» Rounded random numbers
» Random Pronounceable Passwords
» Pick Randomly from Array
» Weighted Random Choice

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

    $path_to_images = "/images/";  // path to your images
    $default_img = "image.gif";  // image to display if directory listing fails
    FUNCTION getRandomImage($thispath, $img) {
        IF ( $list = getImagesList($thispath) ) {
            MT_SRAND( (double)MICROTIME() * 1000000 );
            $num = ARRAY_RAND($list);
            $img = $list[$num];
        RETURN $thispath . $img;
    FUNCTION getImagesList($thispath) {
        $ctr = 0;
        IF ( $img_dir = @OPENDIR($thispath) ) {
            WHILE ( FALSE !== ($img_file = READDIR($img_dir)) ) {
                // can add checks for other image file types here
                IF ( PREG_MATCH("/(\.gif|\.jpg)$/", $img_file) ) {
                    $images[$ctr] = $img_file;
            RETURN $images;

Oracle Magazine
and Oracle White Papers

SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 13 users online    © 2009 psoug.org

Forgot your password?