newbie: scandir _not_ including directories

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a function below that I (partially) wrote to handle a
particular situation.  

I'm working with horse records in text files. I want to list all the
files with horse information in them, then a dash "-", then, a brief
description of the horse (taken from a separate text file). If that
description isn't in the text file, it leaves a blank line (so I know
to add it). The lines of extra information, as listed in separate
file, do not have to be in any order.

This all works:

I find that that scandir() also scans for subdirectories. I _only_
want to scan for .txt files. Scandir is good for me to use because it
alphabetizes the files without any additional work.

I would appreciate any help that could be offered as far as ways to
adjust my $files array so it's only made up of .txt files.

Thank you for reading this post!


<!-- Start PHP Functions -->

// Links creating script taken from:
// Originally added October 21st, 2007
// Modified by Jenny Purcell to be a subroutine to call it multiple
// Further modified to include info on each horse from a separate file
(by directory).

function make_links($dir,$breed) { // create a user-defined function
named make_links
    $files = scandir($dir) ;
    $breedfile = @fopen("$breed","r",4096);
    $count = -1 ; // for array
    if ($breedfile) { // If file exists, execute while
        while (!feof($breedfile)) { // while we haven't reached
the End of File, read lines into the buffer.
           $count = $count + 1 ; // increment
//           $horses[$count][1] = "";
           $horses[] = explode(" - ",fgets($breedfile)); //
separate line elements into a 2 dimensional array    
        } // close while
    fclose($breedfile); // close file when it reaches the EOF
    } // close if
    foreach($files as $ind_file){ // start outer foreach loop
        $letter = strtoupper(substr($ind_file,0,1));  
        if($ind_file != '.' && $ind_file != '..') { // start
2nd if loop
        $count2 = -1 ;    
        $temp = "";
        while ($count2 < $count) { // start inner foreach  
            $count2 = $count2 + 1 ;
            if ($horses[$count2][0]== $ind_file) { //
start 1st if loop
                $temp = $horses[$count2][1]; // temp
is the info on the horse file
            } // end 1st if loop
    } // end inner foreach
            <li><a name="<?php echo $letter; ?>"
href="<?php echo "$dir"."/".$ind_file; ?>"><?php echo $ind_file;?></a>
<?php echo " - $temp" ;?></li>  
        } // end 2nd if loop
    } // end outer foreach loop

} // end user-defined make_links function

<!-- End PHP Functions -->
Jenny Purcell
Snowy River Farms (PBEM Fantasy Horse Racing)

Re: newbie: scandir _not_ including directories wrote:
Quoted text here. Click to load it

I think you have 2 choices: (1) scandir, then use is_dir() on each name to
discard sub-directories, or (2) if you really want 'names ending in .txt'
(which isn't the same as saying 'only text files'), use glob() and sort the
result yourself.

Re: newbie: scandir _not_ including directories

Quoted text here. Click to load it

Here is a sample code will guide you;
This code scans within the chosen directory, returns the content which
ends with "txt".

$MySuggestion="Switch to MySQL buddie!;)";

function isHorseTxt($filepath){

    for($i=0; $i<$cf; $i++){
          $istxt=substr("$file[$i]", 0, -3);
                  echo("$file[$i]"); // i echoed here, or return

Site Timeline