random images from folder

Discussion in 'PHP' started by areaba, Jul 23, 2009.

  1. #1
    Hi, I am using the script random image show from the folder but its not showing the image. plz. help me to solve this issue.
    I am placing the script now .
    function select_random_file_funny_image($dir) {
        if (is_dir($dir)) {
            if ($HD = opendir($dir)) {
                $files = array();
                while (false !== ($file = readdir($HD))) {
                    if (is_file($dir.$file) && $file != basename($_SERVER['PHP_SELF'])) $files[] = $file;
                }
                closedir($HD);
                if (is_array($files)) 
                {
                   shuffle($files);      
                   $file = array_pop($files);    
                   $picpath	=	str_replace(".jpg", "j", $file);
    				$picpath	=	str_replace(".gif", "g", $picpath);
    			   echo '<a href=funny-picture-'.$picpath.'.html>
    			   <img src="thumb.php?pic='.$dir.$file.'&amp; w=96&amp; h=94" border=0></a>';
                }else { echo"";}
            }
        }
    } 
    
    $image_dir ="fun/pictures/";
    select_random_file_funny_image($image_dir);
    PHP:

     
    areaba, Jul 23, 2009 IP
  2. twitterscores

    twitterscores Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    is thumb.php script in the same folder of this script?
     
    twitterscores, Jul 23, 2009 IP
  3. areaba

    areaba Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yes its in the same folder.
     
    areaba, Jul 23, 2009 IP
  4. areaba

    areaba Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can you plz tell me whats the problem in this script.
     
    areaba, Jul 23, 2009 IP
  5. areaba

    areaba Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Waiting for anyone who will solve this problem.
    Thanks
     
    areaba, Jul 23, 2009 IP
  6. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Is it showing anything at all, any errors etc...

    You have a problem here:
    
    echo '<a href=funny-picture-'.$picpath.'.html>
    <img src="thumb.php?pic='.$dir.$file.'&amp; w=96&amp; h=94" border=0></a>';
    
    PHP:
    1. It's best to surround attributes around double quotes, like href="blah.html", src="blah.jpg"...
    2. There are no such attributes such as "w" and "h" for images, i'm assuming you meant to use width and height here.
    3. You have included &amp; twice for some unknown reason, cause they don't seem as though they should be there.
    4. You never actually close your SRC link for the image with a quote.

    I've corrected that below, anyhow, in addition to adding echo statements which will help debug any further errors

    
    <?
    function select_random_file_funny_image($dir) {
        if (is_dir($dir)) {
            if ($HD = opendir($dir)) {
                while (false !== ($file = readdir($HD))) {
                    if (is_file($dir.$file) && $file != basename($_SERVER['PHP_SELF'])){
    					echo "<h3>The File $file was added to the array</h3>";
    					$files[] = $file;
    				}
                }
                closedir($HD);
                if (is_array($files)) 
                {
                   shuffle($files);      
                   $file = array_pop($files);    
                   $picpath =    str_replace(".jpg", "j", $file);
                   $picpath    =   str_replace(".gif", "g", $picpath);
    				echo "<h3>The File is $file and the path is $picpath</h3>";
                   echo '<a href="funny-picture-'.$picpath.'.html">
                   <img src="thumb.php?pic='.$dir.$file.'" width="96px" height="94px" /></a>';
                }
    			else{
    				echo "<h1>It Seems the Directory Exists but no Files were Found</h1>";
    			}
            }
    		else{
    			echo "<h1>It seems false was returned when opening the DIR<h1>";
    		}
        }
    	else{
    		echo "<h1>The DIR supplied is no a valid Directory</h1>";
    	}
    } 
    
    $image_dir ="fun/pictures/";
    select_random_file_funny_image($image_dir);
    ?>
    
    PHP:
    This way you are guaranteed an output and can debug step by step. Try that and let me know what the output is.
     
    wd_2k6, Jul 23, 2009 IP
  7. areaba

    areaba Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks to help me to solve this problem. Sorry i was ill , did not reply you. Anyway its show the output in that way.

    The File fp1.jpg was added to the array
    The File fp10.jpg was added to the array
    The File fp11.jpg was added to the array
    The File fp12.jpg was added to the array
    The File fp13.jpg was added to the array
    The File fp14.jpg was added to the array
    The File fp15.jpg was added to the array
    The File fp16.jpg was added to the array
    The File fp17.jpg was added to the array
    The File fp18.jpg was added to the array
    The File fp19.jpg was added to the array
    The File fp2.gif was added to the array
    The File fp20.jpg was added to the array
    The File fp21.jpg was added to the array
    The File fp22.jpg was added to the array
    The File fp23.jpg was added to the array
    The File fp24.jpg was added to the array
    The File fp25.jpg was added to the array
    The File fp26.jpg was added to the array
    The File fp27.jpg was added to the array
    The File fp28.jpg was added to the array
    The File fp29.jpg was added to the array
    The File fp3.jpg was added to the array
    The File fp30.jpg was added to the array
    The File fp31.jpg was added to the array
    The File fp32.jpg was added to the array
    The File fp33.jpg was added to the array
    The File fp34.jpg was added to the array
    The File fp35.jpg was added to the array
    The File fp36.jpg was added to the array
    The File fp37.jpg was added to the array
    The File fp38.jpg was added to the array
    The File fp39.jpg was added to the array
    The File fp4.jpg was added to the array
    The File fp40.jpg was added to the array
    The File fp41.jpg was added to the array
    The File fp42.jpg was added to the array
    The File fp43.jpg was added to the array
    The File fp44.jpg was added to the array
    The File fp45.jpg was added to the array
    The File fp46.jpg was added to the array
    The File fp47.jpg was added to the array
    The File fp48.jpg was added to the array
    The File fp49.jpg was added to the array
    The File fp5.jpg was added to the array
    The File fp50.jpg was added to the array
    The File fp51.jpg was added to the array
    The File fp52.jpg was added to the array
    The File fp53.jpg was added to the array
    The File fp6.jpg was added to the array
    The File fp7.jpg was added to the array
    The File fp8.jpg was added to the array
    The File fp9.jpg was added to the array
    The File Observing Data on Screen.jpg was added to the array
    The File Sample.jpg was added to the array
    The File Thumbs.db was added to the array
    The File is fp52.jpg and the path is fp52j

    One thing i want to tell you. that this script is working good in my previous server. the problem i have faced when i have changed my server. May be some setting problems also exists. Can you tell me about that.
    Thanks
     
    areaba, Jul 27, 2009 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8
    show the source generated by the script - in particular, the img src="thumb.php? etc bits. then copy one of them and paste it into a new browser/tab, it would go as:

    domain/thumb.php?pic=fun/pictures/fp21.jpg - see if it looks anything like this, see if the url works, if thumb.php opens the file ok, what headers it produces and so forth. it may be that it cannot access the folder to pipe the data, things like absolute path and safe mode can do this (change in hosting can cause this)

    ALSO

    width="96px" height="94px" /> - do not put px after the values, this is not css.

    and finally, array_rand() is a better / faster way of getting a file.
    remove the shuffle($files) then:

    $file = $files[array_rand($files)];
     
    dimitar christoff, Jul 27, 2009 IP
  9. areaba

    areaba Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Dimi thanks to participate . actually this script is working good in my previous server but when i have changed my server then this problem exists. can you tell me about that.

    Thanks
     
    areaba, Jul 27, 2009 IP
  10. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #10
    i did. you need to test that:

    1. the input to thumb.php is correct
    2. thumb.php actually works
     
    dimitar christoff, Jul 27, 2009 IP
  11. areaba

    areaba Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    areaba, Jul 27, 2009 IP
  12. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
  13. areaba

    areaba Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    in funadd.com you can see there was a left baar, today funny picture and today wallpaper. actually in that boxes this script is shown . when you open this site in internet explorer you can see a cross in that box when you click this cross sign the big image you can see that. actually its the same image which show in the box , i am resizing it. so after click the link then it shows the large image. i did not understand why it did not show in that box.
     
    areaba, Jul 27, 2009 IP
  14. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #14
    BECAUSE THUMB.PHP IS BROKEN / CAN'T FIND / READ THE IMAGE AS I CLEARLY SHOWED IN PREVIOUS POSTS.

    please understand that 'it used to work' has no real meaning in IT. good luck to you, one can only do so much to help people that don't want to be helped...
     
    dimitar christoff, Jul 27, 2009 IP