Jump to content

[SOLVED] Trying to create a search result on a file directory


Xianoth

Recommended Posts

I am in need of guidance and help.

 

 

I am working on a project where I need to take a variable from a form, then match that variable against a directory list and return a modified list of files available that are clickable.

 

I have created the form - (easy part)

and I have found coding that pulls the directory list.

 

I am having trouble finding information on how to take that directory list a sort the results based only on the keyword that was typed in.

 

Any help or direction will be appreciated.

 

 

Thank you for your time.

 

Link to comment
Share on other sites

Im assuming u are seeing if the keyword matches the filename and not its contents. If not tell me and ill modify the script.

 

The first file:

<?php
if(isset($_POST['input'])){
$input = $_POST['input'];
$dir = 'mydir';
$dirHandle = opendir($dir);
while($files = readdir($dirHandle)){
	if($files == '.' or $files == '..'){ continue; }
	if(strstr($files, $input)){
		echo "<a href=\"showFiles.php?dir=$dir&file=$files\">$files</a>";
	}
}
closedir($dirHandle);
}
?>

 

showFiles.php

<?php
if(isset($_GET['dir']) and isset($_GET['file'])){
$file = $_GET['dir'] . "/" . $_GET['file'];
$handle = fopen($file , 'r');
$contents = fread($hande, filesize($file));
echo $contents;
fclose($handle);
}
?>

 

What the snippet does, is read a directory and show all the files that contain the keyword. When the user clicks a file, he is redirected to showfiles.php where he can see the contents of that file. Hope this is what u wanted.

Link to comment
Share on other sites

Ok

 

I tried out this script..

 

the first snippet is giving me an error code:

 

Parse error: syntax error, unexpected $end in /usr/local/apache/htdocs/dir7.php on line 15

 

the code is 14 lines..

 

any suggestions?

 

Link to comment
Share on other sites

well. it wasnt working for me, however I was able to take the code suggestions and manipulate a solution as follows:

 

<?php

$input =$_POST['ext'];

$file_dir="./monitor/";

$dir=opendir($file=readdir($dir))

{

      if ($file != "." && $file != "..")

              if(strstr($file, $input))

{

    echo "<a href=".$file_dir."/".$file." target=_blank>".$file."</a>

    echo "<br />;

}

}

?>

 

 

 

Now I know right off that I can mix the </a><br /> together and not have the second echo command, but I am just learning PHP and I am bastardizing several code snippet references together.

 

What this code is doing for me is taking the value from a form, the value coming in is "ext" , based on that the code then does a directory list and uses the strstr command to show only the value that matches "ext", then returns that entry as a clickable url link.

 

My next step is pushing the return to a cgi script, but I think I have that already worked out. Just need to fine tune it.

 

I thank everyone who helped out, and I hope that someone down the road will be able to use this to further their php learning experience.

 

Link to comment
Share on other sites

Glad its working. I see u marked this topic as solved, but anyway im just writting two or three additional things to make u understand it better:

 

The code opens a directory and every file which contains $input is shown. Strstr() finds the first occurrence of a string and if it is found then echo. U may consider converting the $file to lowercase for improved results:

if(strstr(strtolower($file), $input)

 

That was all. Good luck with your script.

Link to comment
Share on other sites

  • 2 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.