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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

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

Post 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>";

?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

a better way?

Post 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?
RTT
Forum Commoner
Posts: 38
Joined: Thu Jul 17, 2003 10:22 am
Location: Wolverhampton, UK
Contact:

Post by RTT »

How does that pull up all the files?
grep is a unix shell command!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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>";
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Thanks

Post 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.
Post Reply