Page 1 of 1

searching a dir with grep and making a list of links to file

Posted: Thu Nov 20, 2003 9:21 am
by mccommunity
I am trying to make a web page that searches a directory pulling up a link list to all files (.txt, .html, .doc) that are located in the dir. I want the links to be clickable and it to spawn the document. Here is what I have so far. It searches the dir but jumbles everthing together and when you click the links it does not work. How could I make each entry on a single line and make it so when you click that entry it pulls the document? Thansk.


Mark

Code: --------------------------------------

<?php

$word = "online"; // word to search
$opt = "-ilr"; // ignore case, list files with matches, search recursive
#$path = "/home/client/public_html/";

$result = `grep $opt $word ./\n`;

echo "<a href = \"$result\">$result</a><BR>";

?>

Posted: Thu Nov 20, 2003 9:33 am
by JayBird
eh?

that code looks weird mate, there is no grep function in PHP. There is a preg_grep.

Basically, the code you have post makes no sense.

Mark

a better way?

Posted: Thu Nov 20, 2003 10:15 am
by mccommunity
This pulls up a listing off all the files, I just cant make them open, is there a better way to do this?

Posted: Thu Nov 20, 2003 10:23 am
by RTT
How does that pull up all the files?
grep is a unix shell command!

Posted: Thu Nov 20, 2003 10:23 am
by Weirdan
Bech, if you don't understand the code, read the manual.

mccommunity, you have to [php_man]explode[/php_man] the result. Then, probably, you need to strip './' from the beginning of the lines...

Code: Select all

//...skipped...
$result = `grep $opt $word ./\n`; 
$result = explode('\n',$result);
foreach($result as $filename)
  echo "<a href = "$filename">$filename</a><BR>";

Thanks

Posted: Thu Nov 20, 2003 10:50 am
by mccommunity
Thanks Wierdan.... it worked but I had to explode the ./ instead of the \n thank you very much for your help on this.