Forum Moderators: coopster

Message Too Old, No Replies

PHP file search

         

andrewsmd

3:49 pm on Aug 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a web page that takes in a path and searches for files based on user input. An example would be the user entering C:\folder_name\*.txt now my php will output all files in C:\folder_name\ with anything and then the extension .txt. I use the glob function to search because it works fantastic. Here is my problem, some folders with which this program will run are extremely big (over 10s of 1000s of files). I would like to return maybe 20 or 30 names unless the user specifies otherwise. I can do that with a simple counter, but I want to display the most recent file names. Meaning if a.txt a1.txt a2.txt were modified on 08/08 and d.txt d1.txt d2.txt were modified on 08/09 I want them to take priority over the a files because they are more recent. So is there anyway to sort the glob array based on the most recently updated files. Here is the basic syntax of how my output works. I made it as simple as possible for reading sake.

$arrayPath = glob("C:\folder_name\*.txt");

foreach($arrayPath as $i){

echo($i);

}

eelixduppy

4:02 pm on Aug 19, 2008 (gmt 0)



You might be better off with a Unix command. Maybe something like a ls -t and then you grab your results that way.

andrewsmd

6:40 pm on Aug 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do I use Unix with php. I have not done that at all

eelixduppy

11:36 pm on Aug 19, 2008 (gmt 0)



Well, first off I'm assuming you are running some sort of Unix machine, yes? If that's the case, you should be able to run the command that I have shown above using the execution functions [php.net] that PHP has to offer. Take a look.

andrewsmd

12:15 am on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh well for some permissions reasons I can't use the execution functions. I have already ran into that problem a long time ago.

eeek

9:44 am on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You might be better off with a Unix command.

Why would forking and exec'ing a command be better than just doing a glob() or readdir()?

andrewsmd

12:13 pm on Aug 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The glob function reads all of the files and sorts them by name. e.g. a.txt b.txt c.txt d.txt etc. I only output 30 files because some of these folders we search have thousands of files. I want to output the most recent files because 99% of the time that is what we are dealing with. Files that have been modified recently. So even though glob would put d.txt after a.txt if d.txt was modified on 08/20 and a.txt was modified on 04/20 I want d.txt to be in the array first.