reading a dir with PHP
Results 1 to 10 of 10

Thread: reading a dir with PHP

  1. #1
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447

    reading a dir with PHP

    ok I am not a php coder by any stretch of the imagination - i can muddle my way through someone elses code and have a basic understanding but thats about it.

    Anyways on to my question :

    What am looking to do is have php read a dir containing pictures and let choose say 16 at random and output them like

    Code:
    <img src=http://somesite.com/images/random1.jpg>
    <img src=http://somesite.com/images/random2.jpg>
    <img src=http://somesite.com/images/random3.jpg>
    <img src=http://somesite.com/images/random4.jpg>
    <br />
    <img src=http://somesite.com/images/random5.jpg>
    <img src=http://somesite.com/images/random6.jpg>
    <img src=http://somesite.com/images/random7.jpg>
    <img src=http://somesite.com/images/random8jg>
    and so on so it builds a 4x4 square using the pictures

    anyone wanna knock up something for me?

    v_Ln

  2. #2
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Unfortunately, I currently have no time to write the whole code
    (with an arbitrary directory somewhere and arbitrary pictures),
    but if the directory is fixed and the pictures have names
    like random?.jpg, you can use something like

    Code:
    <?php
    $filename = "http://somesite.com/images/random".rand(1,64).".jpg"; // assuming 64 pictures
    echo("<img src=".$filename.">...);
    	
    ?>
    Otherwise, I would
    1) fill an array with the file-names array_names,
    2) implement a for-loop from 1-4 and another from 1-4
    3) in which I choose $randompicture_name=array_names[rand(1,nr_of_pictures)];
    4) echo("<img src=...$randompicture_name...>");

    If none will convert this into php, I will next week

    Cheers!
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  3. #3
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    the second one sounds like the idea

    the image directory will be populated by pictures uploaded by other people so setting a static variable for number of pictures wouldn't work which is a shame - nor will i know the filenames of each picture.

    but it def looks like your second suggestion is on the right tracks!

    v_Ln

  4. #4
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    the second one sounds like the idea
    good

    Well, the enumeration is dynamic, as well as nr_of_pictures is a variable,
    hence not a static number. A enumeration of the files is something like

    Code:
    ...
    			$handle=opendir($dirname); 
    			while ($file = readdir($handle)) 
    			{ 
    				if($file=='.'||$file=='..') continue; 
    			
    				if(!is_dir($dirname.$file)) {
                                             
    					$temp = explode(".", $file); 
    					if (isset($temp[1])) if ($temp[1]=="jpg") {
                                                     nr_of_pictures++;
    //						$temp = explode(".", $file);$file=$temp[0];
    						....
    					}
    				} else continue;
    			}
    			closedir($handle); 
    ...
    Almost there. Sorry, but I just wrote that now in a couple of minutes
    (using resources like [1]) so it might have errors etc. but be useful ...

    It is a shame: it is a sunday and I am working - so, no time to test and
    finish this completely.

    cheers!

    [1] http://php.net/opendir
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  5. #5
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    your the man - lol
    thanks alot for all your continuing help with this

    v_Ln

  6. #6
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    oh one thing i forgot to add ::

    when picking the image to display at random it also needs to check which images have already been picked so the same image doesn;t show up in each 4x4 set more than once

    v_Ln

  7. #7
    AO French Antique News Whore
    Join Date
    Aug 2001
    Posts
    2,126
    Not sure if I'll help but here is my code to look in a directory. It look throw all level 1 sub directory for a file call setting.php. If the file is found in the directory, it use the setting to output the data. It's not random.

    Images.php Code
    PHP Code:
    <head>
    &
    lt;META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    &
    lt;title>Dagang Photo</title>
    &
    lt;/head>
    &
    lt;?php
      
      
    // in case register global is off
      
    foreach($HTTP_GET_VARS as $key =&gt$val) {
         $
    $key $val;
         global $
    $key;
      }
      global 
    $files$isDir$g_twidth$g_cols$g_rows$g_title$g_desc$g_dispFn;
      include 
    "Menu.php";
      
    $path adjustPath($path);
      if (
    file_exists("$path/setting.php")) {
        include(
    "$path/setting.php");
      }

      if(!
    $page)
        
    $page 0;


      if(!
    $g_cols)
        
    $g_cols 1;
      if(!
    $g_rows)
        
    $g_rows 100;
      if(!
    $g_twidth)
        
    $g_twidth 200;

      @
    readFiles($path);
    ?&
    gt;


    &
    lt;div align="center">
    &
    lt;table border="0" cellspacing="0" cellpadding="10">
        &
    lt;tr>
            &
    lt;td>

            &
    lt;div align="center" class="desc"><font face="Arial" size="3"><b><?php echo $g_desc; ?></b></font></div>
            &
    lt;p>
            &
    lt;div align="center">
            &
    lt;?php
                
    @showPageLinks($pathcount($files), $page$img,
                    
    $g_cols$g_rows);
            ?&
    gt;
            &
    lt;/div>
            &
    lt;/td>
        &
    lt;/tr>
        &
    lt;tr>
            &
    lt;td>
            &
    lt;?php
                
    if (!$img) {
                  @
    showImgTable($path$files$isDir$page,
                    
    $g_cols$g_rows);
                } else {
                  @
    showBigImg($path$img);
                }
            ?&
    gt;
            &
    lt;/td>
        &
    lt;/tr>
        &
    lt;tr>
            &
    lt;td>

            &
    lt;?php
                
    @showPageLinks($pathcount($files), $page$img,
                    
    $g_cols$g_rows);
            ?&
    gt;
            &
    lt;/td>
        &
    lt;/tr>
    &
    lt;/table>
    &
    lt;/div>
    &
    lt;? include "End.php"; ?&gt

    Setting File

    PHP Code:
    <?php
    // description 
    $g_desc='Descp  ';
    // number of columns per page
    $g_cols='1';
    // number of rows per page
    $g_rows='20';
    // display file name?
    $g_dispFn='check';
    // sort by filename
    $g_sort_by_filename=true;
    global 
    $g_title$g_desc$g_cols$g_rows$g_twidth$g_bgcolor$g_titlecolor$g_desccolor$g_dispFn,   $g_sort_by_filename;
    ?&
    gt
    -Simon \"SDK\"

  8. #8
    Banned
    Join Date
    Sep 2004
    Posts
    305
    I don't have a server to test this on nor the skill to code jack **** so enjoy! Fix it if it's wrong and post it here, I could sure use one of these scripts:

    Code:
    <?php
    $line_break = 0;
    $images_picked = 0;
    
    if ($handle = opendir('/path/to/files')) {
       while (false !== ($file = readdir($handle))) {
           $pics_in_dir[] = $file;
       }
    }
    
    while($images_picked != 16) {
    	if($line_break % 3 == 0) {
    		echo("<br>");
    	}
    	echo(random_pic());
    }
    
    function random_pic() {
    	$random_number = rand(0,count($pics_in_dir)-1); 
    	$random_image = "<img src=\"".$pics_in_dir[$random_number]."\" width=5 height=5>";
    	if(in_array($random_image, $used_images)) {
    		random_pic();
    	}
    	else {
    		$used_images[] = $random_image;
    		$images_picked++;
    		$line_break++;
    		return($random_image);
    	}
    }
    Have a glorious day.

  9. #9
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi Valhallen

    I haven't forgotten my promise. Here is the complete code, based on
    my initial idea (with the addition of "unique pictures") - tested. Maybe
    ;TT's script is more elegant, but would need some revision

    Code:
    <?php
    
    $dirname="pictures\\"; 					// relative path of the pictures to the position of this script (or its include)
    							// don't forget the path delimiter "\\"
    srand ((double)microtime()*1000000); // initialisation of the random generator
    
    $nr_of_pictures=0;	
    // create the list of files
    
    	$handle=opendir($dirname); 
    	while ($file = readdir($handle)) { 
    
    
       	   if(!is_dir($dirname.$file)) {
    	
           			$temp = explode(".", $file);
    			$sot=sizeof($temp);		
    		if ($sot>1){				// there is at least one  extension
    		
    			$ext=$temp[$sot-1];		// the last extension is the relevant one
    		
    	                if (($ext=="jpg") || ($ext=="gif")){    // if the file type is jpg or gif
    
    			  $pictures[$nr_of_pictures]=$file;     // array with the file names	
    			  $picture_used[$nr_of_pictures]=0;     // array for the uniqueness-check in the second step
                              $nr_of_pictures++;		
                            } // if extensions
    		
    		} // if sizeof
     	   } // if is_dir
    	} // while
    
    	closedir($handle); 
    
    // create the output
    
    if ($nr_of_pictures>=16){                        // enough pictures available?
    
      for ( $row = 1; $row <= 4; $row++) {
         for ( $column = 1; $column <= 4; $column++) {
    
      	  $r=rand(0,$nr_of_pictures-1);
     	  while($picture_used[$r]) $r=rand(0,$nr_of_pictures-1);	// makes sure that each picture is unique
    
    	  $picture_used[$r]=1;
    	  echo("<img src=\"".$dirname.$pictures[$r]."\" height=\"40\" width=\"40\"></a>");
    	
         } //for $column
         echo("<br>");
      } //for $row
    } // if enough pictures
    
    	
    ?>
    a tip to optimize bandwidth
    This depends on what you intend to do but -

    Do not show the picture, but a "thumbnail"-version of it.

    You could do this
    a) by making "manually" sure that a thumbnail-version exists - and you
    use only these for the output
    b) you modify the php script such that it generates the thumbnail-version
    "on the fly" if the thumbail version does not exist.


    Cheers!

    P.s. What happened with the flash game you wanted to design using the
    parabolic trajectory of some object[1]?

    [1] http://www.antionline.com/showthread...hreadid=264576
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  10. #10
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    nice one sec works perfectly - had to tweek the dir bit tho the '\\' wasn't working but when i changed it to '/' it worked - dont ask me why dont have alot of php knowledge - as you prolly guessed

    will send you a link to finished page using script when its done (gimmie a couple of days)

    as for the flash game thats been put off in the meantime until i get this project finished but will keep u updated.

    v_Ln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •