Last file in folder function
Posted: Fri Aug 08, 2008 11:01 am
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:
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!
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++
}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!