Xianoth Posted August 27, 2007 Share Posted August 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 27, 2007 Share Posted August 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
Xianoth Posted August 27, 2007 Author Share Posted August 27, 2007 its a start. What happens is the clickable output will redirect to a cgi script.. Let me try this out and I will let you know whats going on. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
Xianoth Posted August 27, 2007 Author Share Posted August 27, 2007 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? Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 27, 2007 Share Posted August 27, 2007 The first script is working, i tried it. I googled your errors and i found it is due to short tags <? ?>. Dont know really.. Quote Link to comment Share on other sites More sharing options...
Xianoth Posted August 27, 2007 Author Share Posted August 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 27, 2007 Share Posted August 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
Xianoth Posted August 28, 2007 Author Share Posted August 28, 2007 Thanks for the tip.. I will add that and check the results. I appreciate the help and advice. Quote Link to comment Share on other sites More sharing options...
panado Posted November 25, 2009 Share Posted November 25, 2009 Morning All, I am new to PHP, this script is exactly what I am looking for, I do have one question though. Can I limit the files I am looking for, i.e. I only want to show the .pdf files or the .jpg files that match ? thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.