Page 1 of 1
Seachable Text files in directory
Posted: Thu Oct 23, 2003 6:00 pm
by wesnoel
OK here is what I want to do...
I would like to read a dir and only list certain files.
Is there a way to loop through all of the txt files in the directory and find a word to match. If the word exists in the files it displays those files and all of the other files will not display.
Any direction would be helpfull.
Thanks Wes/
Posted: Thu Oct 23, 2003 7:23 pm
by Gen-ik
Code: Select all
<?
$find = "sometext";
$directory = "files";
$d = dir($directory);
while(($file = $d->read())!==false)
{
if($file!="." && $file!=".." && stristr($file, ".txt")!==false)
{
$open = fopen($directory."/".$file, "rb");
$read = fread($open, filesize($directory."/".$file));
fclose($open);
clearstatcache();
if(stristr($read, $find)!==false)
{
$matches[] = $directory."/".$file;
}
}
}
foreach($matches as $path)
{
echo "File Match : <a href="{$path}">{$path}</a><br>";
}
?>
That should work...... I've just typed it as a reply so I haven't tested it out yet..... that's gonna be your job

Posted: Thu Oct 23, 2003 7:43 pm
by DuFF
This will search the current directory for text files. Then it will read through each text file looking for the keyword specified.
Code: Select all
<?php
<?php
function findword($keyword) {
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
$ext=explode('.',$file);
if ($file != "." && $file != ".." && $ext[1]=='txt') {
$filearray[] = $file;
}
}
closedir($handle);
}
for($x = 0; $x < sizeof($filearray); $x++)
{
$lines = file($filearray[$x]);
foreach ($lines as $line_num => $line) {
if (preg_match ("/$keyword/i", $line)) {
echo $filearray[$x];
}
}
}
}
findword(hello); //usage
?>
?>
Posted: Fri Oct 24, 2003 10:51 am
by wesnoel
Gen-ik--
I have tried your code and I keep getting an error...
Warning: Invalid argument supplied for foreach() in /var/www/dev/allfiles.php on line 25.
I have messed a bit with it but I can't get it to stop giving me that error.
Wes/
Posted: Fri Oct 24, 2003 12:32 pm
by Gen-ik
Ok try this version.........
Code: Select all
<?
$find = "sometext";
$directory = "files";
$d = dir($directory);
while(($file = $d->read())!==false)
{
if($file!="." && $file!=".." && stristr($file, ".txt")!==false)
{
$open = fopen($directory."/".$file, "rb");
$read = fread($open, filesize($directory."/".$file));
fclose($open);
clearstatcache();
if(stristr($read, $find)!==false)
{
$matches[] = $directory."/".$file;
}
}
}
echo "<b>".count($matches)."</b> matches have been found.<hr>";
if(count($matches)>0)
{
foreach($matches as $path)
{
echo "File Match : <a href="{$path}">{$path}</a><br>";
}
}
?>
Posted: Fri Oct 24, 2003 1:07 pm
by markl999
If you have PHP 4.3.0 or above you could do:
Code: Select all
$find = 'searchtext';
$directory = '/path/to/the/dir/';
foreach(glob($directory.'*.txt') as $txtfile){
if(strpos(file_get_contents($txtfile), $find)){
echo 'Found in: '.$txtfile.'<br />';
}
}
Posted: Fri Oct 24, 2003 2:28 pm
by d3ad1ysp0rk
from looking at duFF's code, im assuming Function is like "class" in Java?
Posted: Sat Oct 25, 2003 7:39 pm
by wesnoel
Thats great guys I got it working with Gen-ik's code.
Thank to all of you for taking the time for a noob.
you all rock!
Wes/