Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading file names into an Array 2

Status
Not open for further replies.

lyallk

IS-IT--Management
Jul 26, 2004
4
US
Kinda a newbe to php here, but i am working on that.

I need a rotating picture at the top of a webpage. I need to do this without a db (ie mysql). I am trying to modify some code i found (props for the code-->Clicky).

Code:
srand((float) microtime() * 10000000); 
  $image[1]='/location/of/image1.jpg'; 
  $image[2]='/location/of/image2.jpg'; 
  $image[3]='/location/of/image3.jpg'; 
  $image[4]='/location/of/image4.jpg'; 
  $image[5]='/location/of/image5.jpg'; 
  $rn = array_rand($image); 
echo '<img src="'.$image[$rn].'">';

What i would like to do, is count the number of files in a dir and the load an array with the file names (ie $image[1]=$file_name_one_blah, $image[2]=$file_name_two_blah), pick a rand num, and display the random pic. Sounds easy right? Its killin me.

Could somebody poke me in the right direction?? Plz!!!
 
This somewhat does what you need. Itopen s a dir and puts all the files into an array.

Code:
$dir="somedirectory";
//check that $dir really exists
    if(is_dir($dir)){
//initialize counter
    $num_files="0";
//initialize files array
    $file_list[0]="..";
//Open Dir to parse
    if ($dh = opendir($dir))
    {
        //begin loop to read directory
        while (($filename = readdir($dh)) !== false){
            //discard . and .. directory pointers
             if (($filename != ".") && ($filename != ".."))
             {
             $num_files++;
             $file_list[$num_files]=$filename;
             }//if . and ..
          }//while
    }
    closedir($dh);

[/dir]

it basically cycles through a dir, adding files to an array, and keeping a variable with the amount of files added.

Hope this helps

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks Thanks Thanks!!! Making my day easier. I will give both of them a shot.

And a star for each!!!
 
You are most welcome.

If you fiddle around with the code, you can specify the types of files you want, and wether or not to add directories to the file list. etc..



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top