Page 1 of 1

Last file in folder function

Posted: Fri Aug 08, 2008 11:01 am
by bsutcliffe
I have written a script that opens all the text files in a folder and writes the first line of the file to the frontend. The files are called 1.txt, 2.txt and the script simply does this:

Code: Select all

$id=1;
 
while(file_exists("folder/" . $id .".txt")) {   
   $txt_file = fopen($file, "r") or exit("Error");
   $first_line = fgets($txt_file);
   echo "The first line is ".$first_line;
 
   $id++
}
This is all well and good but instead of starting with the first file (ie. 1), I would like to start with the last file. How do I get the name of the last file (sorted alphabetically) in a folder so that I can store this as the variable $id (without having to type in 12 or 13 or whatever. I also know that I will have to change $id++ to $id-- (or is it $id+-?).

Also, I would like to know how to read the third line of the text file. To get the second line, I know you can read the first line (ie. fgets to \n) and find out how many bytes the first line is, then start the cursor at that offset to read the second line. A) there must be an easier way and B) if not how do I use this to read the thrid, fourth, sixth line etc. Is there a function anybody has written to readline(linenumber)?

Thanks!

Re: Last file in folder function

Posted: Fri Aug 08, 2008 11:17 am
by ghurtado
For your first problem, I would first read all the files in a directory, and then sort them alphabetically, that is probably the simplest solution.

For your second problem:

Code: Select all

$lines = file('myfile.txt'); // reads file into array
echo $lines[2]; // this is the second line in the file

Re: Last file in folder function

Posted: Fri Aug 08, 2008 11:49 am
by bsutcliffe
Thanks ghurtado.

I have now written the following code, which works.

Code: Select all

               function is_txt($filename){ 
                $filename    =    strtolower($filename) ; 
                $ext        =    split("[/\.]", $filename) ; 
                $n            =    count($ext)-1; 
                $ext        =    $ext[$n];
                if($ext=="txt") { 
                    return true; 
                } else { 
                    return false; 
                } 
          };
                  
          function last_file($curr_dir) { 
                $dir    =    opendir($curr_dir); 
                $array    =    array(); 
 
                while($file = readdir($dir)) { 
                     if(is_txt($file)) { 
                        array_push($array, $file); 
                    }
                }
                
                $array_total = (count($array)-1);
                
                $last_file = $array[$array_total];
            
                $last_file_stripped = str_replace('.txt','',$last_file);
                
                
                return $last_file_stripped; 
          };
          
         
          
          
          $id=last_file('files/');
          
          while(file_exists("files/" . $id .".txt")) {  
                $file = "pfiles/" . $id .".txt";
                $txt_file = fopen($file, "r") or exit("Error");
                
                $lines = file($file);
                
                $pname = $lines[0];
                $dname = $lines[1];
                $dweb = $lines[2];
                $pinfo = $lines[3];
                $pth = $lines[4];
                $plg = $lines[5];
 
                  $id--
           }

Now all I have to do is turn my data in to a funky lightboxy clicky thingy.

Re: Last file in folder function

Posted: Fri Aug 08, 2008 12:29 pm
by ghurtado
Glad to be of help. Thanks for sharing your resulting solution.