View Full Version : search a directory on my site
derekm
07-21-2007, 05:30 PM
I have about 300 word/pdf files that I want to create a search box to search just that directory and pull up all results on the same page. Could someone please help me with this.
Thanks
Derek McIntire
djr33
07-21-2007, 05:52 PM
Might run a bit slow. Using a database with a precompiled list of words might be beneficial.
<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$array[$n] = $file;
$n++;
}
return $array;
}
print_r(searchfiles($search,'mydocs')); //set mydocs to your directory
//$search must be the text to match
?>
The formatting will be a bit off, and the files won't be links or anything, but you should be able to figure that out.
derekm
07-21-2007, 07:26 PM
Thank You
I will try it and see
derekm
07-21-2007, 07:35 PM
where would I put the search box and the submit button in this script?
thetestingsite
07-21-2007, 07:39 PM
You would want to do something like this:
<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$array[$n] = $file;
$n++;
}
return $array;
}
if (isset($_POST['search'])) {
print_r(searchfiles($_POST['term'], 'mydocs')); //set mydocs to your directory
//$search must be the text to match
}
else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
Search Term: <input type="text" name="term"> <input type="submit" name="search">
</form>
<?php
}
?>
Hope this helps.
derekm
07-21-2007, 10:40 PM
I think I know the answer to this ? but If I have php anywhere in my page do I need to save that page with a .php extention
thetestingsite
07-21-2007, 10:43 PM
Yes, or at least set up the server to parse whatever file extension you name it to to serve as a PHP file. On most server installs though, you will want to name it as myfile.php (where myfile is the name of the file).
Hope this helps.
derekm
07-21-2007, 11:05 PM
Thank You for your time
DM
derekm
07-21-2007, 11:11 PM
I did everything and I posted it and this what I am recieving on the page that it is susposed to display
Warning: file_get_contents(php): failed to open stream: No such file or directory in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
This is what is on Line 69 of the php file.
if (!strpos($search,file_get_contents($ord.''.$ordinances.php))) continue;
Could you tell me what I am doing wrong.
DM
djr33
07-22-2007, 01:02 AM
You took out the slash.
$dir.'/'.$file --> dir/file.ext
derekm
07-22-2007, 01:19 AM
Ok Last post for this: sorry to be a such a moron but now I am getting this
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
This is what is on Line 69 of the php file.
if (!strpos($search,file_get_contents($ord.'/'.$ord))) continue;
Could you tell me what I am doing wrong.
DM
thetestingsite
07-22-2007, 01:30 AM
Is the variable "$ord" defined elsewhere in the script? If not, that would be the reason why. I recommend using djr's script; and if needed, using my modification to it.
Hope this helps.
djr33
07-22-2007, 01:35 AM
Well, to start, you're using the same variable twice.
So, that's like searching for directory/directory, or file.ext/file.ext, neither of which will end up giving you any results.
file_get_contents() returns the data in a file on your server. It uses a path to find the file. dir1/dir2/dir3/file.ext is an example of this.
Using $dir.'/'.$file SHOULD return directoryname/filename.extension, assuming $dir and $file are set correctly.
In the script I wrote, both of these are set. I'm not sure why you are changing the variable names. You're welcome to do so, but...
1. Changing the names can cause problems (and likely has) since you need to be completely consistent throughout the script so no references are lost.
2. Using generic names like I did (though unrelated to whether the script works or not, since variable names serve no purpose but identification) is helpful because you know what it is, within the function. Using "$dir" makes more sense than "$images", just as naming a variable "$number" makes more sense than "$one", etc. It's up to you, but I like having generic names within a function. In the rest of your script, do whatever you want (as long as it makes sense, since that's the most important part-- being able to follow what's going on).
At this point, I'd say decide why you are changing the variable names. You could try going back or you could be sure that all instances have changed.
You may want to paste the full source code here (skip all HTML as that won't be helpful).
EDIT: Sorry, testingsite. I was writing while you posted.
derekm
07-22-2007, 01:42 AM
Thanks Both of you Give me a minute and I will redo it to what you originally had and post what I have so for here so you can see what I have
Thanks a bunch
DM
derekm
07-22-2007, 01:52 AM
Ok here is what I have:
<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$array[$n] = $file;
$n++;
}
return $array;
}
if (isset($_POST['search']) )
print_r(searchfiles($_POST['term'], 'ord')); //set mydocs to your directory
//$search must be the text to match
else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?> method="POST">
Search Term: <input type="text" name="term"> <input type="submit" name="search">
</form>
<?php
}
?>
When I post it I go to the form type in a key word to search and I am getting a page not found and the url is:
http://www.mysite.org/mypage.php%20method=?term=annexed&search=Submit+Query
This is what I have can you tell what is wrong
I am kind of getting this a little better so maybe with you guys help I wont have to bother ya much more.
Thanks
DM
thetestingsite
07-22-2007, 02:19 AM
Sorry about that, forgot to add a closing quote after the form action like so:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
That should fix it
derekm
07-22-2007, 02:26 AM
Ok That made it stop giving me the page not found and any errors so we are getting somewhere but no all that it is doing is when i type a word in it just refreshes the page basically and doesnt show any results.
here is what I have
<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$array[$n] = $file;
$n++;
}
return $array;
}
if (isset($_POST['search']) )
print_r(searchfiles($_POST['term'], 'ord')); //set mydocs to your directory
//$search must be the text to match
else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
Search Term: <input type="text" name="term"> <input type="submit" name="search">
</form>
<?php
}
?>
thetestingsite
07-22-2007, 02:34 AM
Not sure if this will work or not, but you could try this:
<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$results[$n] = $file;
$n++;
}
return $results;
}
if (isset($_POST['search'])) {
print_r(searchfiles($_POST['term'], 'ord')); //set mydocs to your directory
//$search must be the text to match
}
else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
Search Term: <input type="text" name="term"> <input type="submit" name="search">
</form>
<?php
}
?>
Changes made are in red. Hope this helps.
djr33
07-22-2007, 03:01 AM
<form action="<?php echo $_SERVER['PHP_SELF'];?> method="POST">
There's no close quote on the action.
I have no idea why it's generating such a string in the URL, but there's no real point in setting the action to that anyway. Using simply action="" will go to the same page.
thetestingsite
07-22-2007, 03:05 AM
Using simply action="" will go to the same page.
I was under the impression that that was invalid, but could be wrong. Anyways, I fixed that in my few posts as well as pointed it out about the closing quote. As for the script refreshing to itself, not sure what would be causing that.
derekm
07-22-2007, 03:16 AM
Ok I know you are probably getting tired of me so hopefully this post will solve it all.
I put the quotes in and now I am getting this
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
And Line 69 is this:
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
And just for further refreshing here is the whole code again:
<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$array[$n] = $file;
$n++;
}
return $array;
}
if (isset($_POST['search']) )
print_r(searchfiles($_POST['term'], 'ord')); //set mydocs to your directory
//$search must be the text to match
else {
?>
<form action="" method="POST">
Search Term: <input type="text" name="term"> <input type="submit" name="search">
</form>
<?php
}
?>
And you said earlier that something about assuming $dir and $file are set correctly, where was i susposed to set that at :)
Thanks
DM
thetestingsite
07-22-2007, 03:31 AM
And you said earlier that something about assuming $dir and $file are set correctly, where was i susposed to set that at :)
It would be on this line:
print_r(searchfiles($_POST['term'], 'ord')); //set mydocs to your directory
The part in red is the directory, the file is found in that directory and "automatically" defined; if that makes sense.
derekm
07-22-2007, 03:35 AM
so your saying I have it right?
thetestingsite
07-22-2007, 03:52 AM
If the directory you are searching in is called "ord", then yes. If not, then you need to change it to the directory you wish to search through.
derekm
07-22-2007, 03:59 AM
well that is the exact directory and it is not working for some reason...
what does "Empty delimiter" mean?
derekm
07-22-2007, 04:08 AM
With this php code
<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$array[$n] = $file;
$n++;
}
return $array;
}
if (isset($_POST['search']) )
print_r(searchfiles($_POST['term'], 'ord')); //set mydocs to your directory
//$search must be the text to match
else {
?>
<form action="" method="POST">
Search Term: <input type="text" name="term"> <input type="submit" name="search">
</form>
<?php
}
?>
I am getting no form at all and getting this on my page..
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
LINE 69
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
Any ideas.....
djr33
07-22-2007, 08:23 AM
I'm not sure if action="" is invalid. If so, just use the name of your page. It's possible to generate this with PHP, but it was giving you errors before, so I don't see the point.
As for the strpos() error, I'm assuming that either $search or file_get_contents() is blank. You can suppress the error using:
if (!@strpos($search,file_get_contents($dir.'/'.$file))) continue;
But I'm not sure if that will help, if something is going wrong.
You're not getting any results at all?
derekm
07-23-2007, 02:51 AM
Ok Here is the web site address. Maybe if you see what it is doing you will be able to tell more about it
www.bayarkansas.org/ordinances.php
Thanks
DM
djr33
07-23-2007, 12:46 PM
Nothing returns results. I assume you have 3 files being searched right now?
The code above should work, if the system is setup correctly (sending the form, etc). However, try changing this:
if (isset($_POST['search']) )
to:
if (isset($_POST['term']))
See if that helps.
derekm
07-23-2007, 01:48 PM
Nothing returns results. I assume you have 3 files being searched right now?
The code above should work, if the system is setup correctly (sending the form, etc). However, try changing this:
if (isset($_POST['search']) )
to:
if (isset($_POST['term']))
See if that helps.
Actually there is a couple hundred files in that directory. But there may only be a few with the term I am searching
derekm
07-23-2007, 01:51 PM
Nothing returns results. I assume you have 3 files being searched right now?
The code above should work, if the system is setup correctly (sending the form, etc). However, try changing this:
if (isset($_POST['search']) )
to:
if (isset($_POST['term']))
See if that helps.
I did what you said above and still got
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
Any Suggestions or am I totally screwed
DM
djr33
07-23-2007, 05:19 PM
Could any of the files have no content?
If so, that could be the reason you are getting the empty message.
Make this line: if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
This:
$fc = file_get_contents($dir.'/'.$file)
if ($fc == '') continue;
if (!strpos($search,$fc)) continue;
derekm
07-24-2007, 01:25 AM
Could any of the files have no content?
If so, that could be the reason you are getting the empty message.
Make this line: if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
This:
$fc = file_get_contents($dir.'/'.$file)
if ($fc == '') continue;
if (!strpos($search,$fc)) continue;
Ok I did what you said but now the page will not load and all that i see is a this
Parse error: parse error, unexpected T_IF in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
derekm
07-24-2007, 01:45 AM
Just so I know we are on the same page the only thing I needed to change in the code below is the dir in print_r(searchfiles($_POST['term'], 'ord')); //set mydocs to your directory
<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$array[$n] = $file;
$n++;
}
return $array;
}
if (isset($_POST['term'])) {
print_r(searchfiles($_POST['term'], 'ord')); //set mydocs to your directory
//$search must be the text to match
}
else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
Search Term: <input type="text" name="term"> <input type="submit" name="search">
</form>
<?php
}
?>
djr33
07-24-2007, 01:54 AM
You should use the new lines of code that I added, to error correct if a file IS empty. However, there was a typo. I forgot a semicolon at the end of a line.
$fc = file_get_contents($dir.'/'.$file);
derekm
07-24-2007, 02:04 AM
$fc = file_get_contents($dir.'/'.$file)
if ($fc == '') continue;
if (!strpos($search,$fc)) continue;
Does the above code need to be exactly like it looks or on one line
djr33
07-24-2007, 02:07 AM
That's right. But again, it's missing a semi colon. Add that to the first line, like in my last post.
derekm
07-24-2007, 02:10 AM
Could any of the files have no content?
If so, that could be the reason you are getting the empty message.
Make this line: if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
This:
$fc = file_get_contents($dir.'/'.$file)
if ($fc == '') continue;
if (!strpos($search,$fc)) continue;
after doing the above i am not getting no warning now;
Now I am getting a refreshed page with no form to fill out?
What next:):):):)
????????
I know I am probably getting on your nerves sorry
DM
tech_support
07-24-2007, 07:02 AM
djr33, you're missing one key point.
300 word/pdf files
derekm
07-24-2007, 07:01 PM
What do you mean tech_support
djr33
07-26-2007, 12:08 AM
I'm not sure how that's related. This will search the source code of those files. .doc format is a big mess, but it does include the actual text in the file too. It might match some of the system stuff too, like if you were to search an html page for the word "body". PDF is a mess, though, and it might not work at all. I'm not sure how this would work.
What I supplied would work for .txt files. It should *mostly* work for .doc and .pdf too. Can't hurt to try.
I'm not sure what you mean about a form.
You should just make a form somewhere. That's not going to magically appear.
$search is the variable used.
At the top of your script, you could put:
$search = $_GET['search'];
And then you can use mypage.php?search=termhere
So you could link to the page like that or do a form like:
<form method="get" action="mypage.php"><input name="search" type="text"></form>
tech_support
07-26-2007, 01:03 AM
What I supplied would work for .txt files. It should *mostly* work for .doc and .pdf too. Can't hurt to try.
A word/PDF file is compressed/encoded in a different format. So you'll just see random ASCII characters or something. Perhaps Wingdings.
You could try making your documents in HTML, and I'm about to finish off a nice search thing. djr33's one searches through quite alot of things, like <title> and PHP tags etc. So that could be a potential security risk.
derekm
07-26-2007, 03:47 AM
A word/PDF file is compressed/encoded in a different format. So you'll just see random ASCII characters or something. Perhaps Wingdings.
You could try making your documents in HTML, and I'm about to finish off a nice search thing. djr33's one searches through quite alot of things, like <title> and PHP tags etc. So that could be a potential security risk.
So Your saying I need to go another route or what I am kinda confused now?!?!?!
djr33
07-26-2007, 04:50 AM
A word/PDF file is compressed/encoded in a different format. So you'll just see random ASCII characters or something. Perhaps Wingdings.This is true with PDFs, though if you look through them some of the data seems to be in English. I don't know much about the format, though.
With word docs, from what I've seen, they are simply like html, just a ton of complex code, but with the actual text embedded in it.
potential security riskNo. It would be a mess, but it wouldn't be risking security, unless you were searching PHP files and other things that can't be served.
As for what you should do with this, I say create a database and search that. Much faster, and easier, and it wouldn't run into any filetype issues. Take the text from the documents and put it into the DB, or something like that.
tech_support
07-26-2007, 06:47 AM
So Your saying I need to go another route or what I am kinda confused now?!?!?!
Hang on... help is on it's way... i'll code as fast as I can... :p
tech_support
07-26-2007, 09:09 AM
Alright. Done.
<?php
function searchfiles($q,$dir) {
//Add the extensions you want to search through here.
$exts = array('.php','.html','.htm','.docx','.doc','.pdf');
//Enter the files that you DON'T want to search through, eg. this page.
$exceptions = array('somefilethatyoudontwant',basename($_SERVER['PHP_SELF']));
//Sets the beginning of the search results and outputs it.
echo '<h2>Your Search Results</h2><ol>';
flush();
//Opens the directory
$handle = opendir($dir);
//Starts a loop through the directory to list the files
while ($file = readdir($handle)) {
//Check if the file is an exception
if (in_array($q,$exceptions)) {
continue;
}
//Check if it's showing '.' meaning this directory (the parent directory) or the directory above it (..)
else if ($file == '.' || $file == '..') {
continue;
}
//Loops through all the extensions
foreach ($exts as $extension) {
//Checks if it's an allowed file extension (eg. Word Documents, HTML Files etc.)
if (strpos($file,$extension)) {
//Gets the file contents...
$c = file_get_contents($dir.'\\'.$file);
//See if there's a match...
if (strpos($c,$q)) {
//Adds the URL
echo '<li><a href="'.$dir.''.$file.'" title="'.$file.'">'.getTitle($c,$file).'</a><br>';
//Gets the body content
echo getBody($c);
//Ends the list item
echo'</li>';
//Outputs it. This allows for the item to be displayed while the page is still loading. Good for when handling large files.
flush();
}
}
//Or else it'll continue through the loop.
else {
continue;
}
}
}
//And close the list.
echo '</ol>';
}
//Tool: stripPHP($c)
//Strips the PHP stuff for you so you can't search it.
function stripPHP($c) {
if (strpos($c,'<?php') != FALSE) {
$php = '';
$php = explode('<?php',$c);
$php[1] = explode('?>',$php[1]);
$c = str_replace($php[1][0],'',$c);
$c = str_replace('<?php?>','',$c);
return $c;
}
else {
return $c;
}
}
//Tool: chopParagraph($str)
//Chops the long content.
function chopParagraph($str) {
$words = 20;
$chopped = '';
$str = explode(' ',$str);
if (count($str)>30) {
for ($i=0; $i <= $words; $i++) {
$chopped.=$str[$i].' ';
}
return $chopped.'...';
}
else {
return $str[count($str)-1];
}
}
//Tool: getBody($content)
//Gets the body content.
function getBody($content) {
$content = stripPHP($content);
if (strpos($content,'<body>')) {
$content = explode('<body>',$content);
$content[1] = explode('</body>',$content[1]);
$returner = strip_tags($content[1][0]);
}
else {
$returner = filterWeirdStuff($content); //For Word Documents...
}
return chopParagraph($returner);
}
//Tool: getTitle($content,$filename)
//Gets the title of the page. If there's no title, it'll display the page name.
function getTitle($content,$filename) {
$content = stripPHP($content);
if (strpos($content,'<title>')) {
$content = explode('<title>',$content);
$content[1] = explode('</title>',$content[1]);
return htmlspecialchars($content[1][0]);
}
else {
return $filename;
}
}
//Tool: filterWeirdStuff($str)
//Filters the ???????????????????***3%^#$^4 you see when trying to get the file contents of Word Documents. Ugh.
function filterWeirdStuff($str) {
$str = explode(' ',$str);
unset($str[0]);
unset($str[1]);
$str = implode(' ',$str);
return '...'.$str;
}
if (isset($_POST['Submit'],$_POST['term'],$_GET['search'])) {
searchfiles($_POST['term'], './');
}
else {
?>
<form action="?search=true" method="post">
<p>
<label>Search: <input type="text" name="term"></label>
</p>
<p>
<label><input type="submit" name="Submit" value="Search"></label>
</p>
</form>
<?php
}
?>
Try check and see if it works for Acrobat Documents. I've got a tonne of homework to do, so I couldn't check it right now.
derekm
07-26-2007, 02:00 PM
Alright. Done.
<?php
function searchfiles($q,$dir) {
//Add the extensions you want to search through here.
$exts = array('.php','.html','.htm','.docx','.doc','.pdf');
//Enter the files that you DON'T want to search through, eg. this page.
$exceptions = array('somefilethatyoudontwant',basename($_SERVER['PHP_SELF']));
//Sets the beginning of the search results and outputs it.
echo '<h2>Your Search Results</h2><ol>';
flush();
//Opens the directory
$handle = opendir($dir);
//Starts a loop through the directory to list the files
while ($file = readdir($handle)) {
//Check if the file is an exception
if (in_array($q,$exceptions)) {
continue;
}
//Check if it's showing '.' meaning this directory (the parent directory) or the directory above it (..)
else if ($file == '.' || $file == '..') {
continue;
}
//Loops through all the extensions
foreach ($exts as $extension) {
//Checks if it's an allowed file extension (eg. Word Documents, HTML Files etc.)
if (strpos($file,$extension)) {
//Gets the file contents...
$c = file_get_contents($dir.'\\'.$file);
//See if there's a match...
if (strpos($c,$q)) {
//Adds the URL
echo '<li><a href="'.$dir.''.$file.'" title="'.$file.'">'.getTitle($c,$file).'</a><br>';
//Gets the body content
echo getBody($c);
//Ends the list item
echo'</li>';
//Outputs it. This allows for the item to be displayed while the page is still loading. Good for when handling large files.
flush();
}
}
//Or else it'll continue through the loop.
else {
continue;
}
}
}
//And close the list.
echo '</ol>';
}
//Tool: stripPHP($c)
//Strips the PHP stuff for you so you can't search it.
function stripPHP($c) {
if (strpos($c,'<?php') != FALSE) {
$php = '';
$php = explode('<?php',$c);
$php[1] = explode('?>',$php[1]);
$c = str_replace($php[1][0],'',$c);
$c = str_replace('<?php?>','',$c);
return $c;
}
else {
return $c;
}
}
//Tool: chopParagraph($str)
//Chops the long content.
function chopParagraph($str) {
$words = 20;
$chopped = '';
$str = explode(' ',$str);
if (count($str)>30) {
for ($i=0; $i <= $words; $i++) {
$chopped.=$str[$i].' ';
}
return $chopped.'...';
}
else {
return $str[count($str)-1];
}
}
//Tool: getBody($content)
//Gets the body content.
function getBody($content) {
$content = stripPHP($content);
if (strpos($content,'<body>')) {
$content = explode('<body>',$content);
$content[1] = explode('</body>',$content[1]);
$returner = strip_tags($content[1][0]);
}
else {
$returner = filterWeirdStuff($content); //For Word Documents...
}
return chopParagraph($returner);
}
//Tool: getTitle($content,$filename)
//Gets the title of the page. If there's no title, it'll display the page name.
function getTitle($content,$filename) {
$content = stripPHP($content);
if (strpos($content,'<title>')) {
$content = explode('<title>',$content);
$content[1] = explode('</title>',$content[1]);
return htmlspecialchars($content[1][0]);
}
else {
return $filename;
}
}
//Tool: filterWeirdStuff($str)
//Filters the ???????????????????***3%^#$^4 you see when trying to get the file contents of Word Documents. Ugh.
function filterWeirdStuff($str) {
$str = explode(' ',$str);
unset($str[0]);
unset($str[1]);
$str = implode(' ',$str);
return '...'.$str;
}
if (isset($_POST['Submit'],$_POST['term'],$_GET['search'])) {
searchfiles($_POST['term'], './');
}
else {
?>
<form action="?search=true" method="post">
<p>
<label>Search: <input type="text" name="term"></label>
</p>
<p>
<label><input type="submit" name="Submit" value="Search"></label>
</p>
</form>
<?php
}
?>
Try check and see if it works for Acrobat Documents. I've got a tonne of homework to do, so I couldn't check it right now.
ok i added that code. I have looked through it. Where do I show what directory that it should be looking in.
tech_support
07-27-2007, 03:05 AM
searchfiles($_POST['term'], './');
Here. Change ./
derekm
07-27-2007, 04:53 AM
searchfiles($_POST['term'], './');
Here. Change ./
Ok I changed that. Now here is what it is doing.
It is giving me results (yippee we are getting somewhere) but here is what it is giving me:
Warning: file_get_contents(ord\Microsoft Word - memorandum- recommended zoning ordinance.pdf): failed to open stream: No such file or directory in /home/content/d/e/r/derekm806/html/formtest.php on line 35
Ok it is giving me this warning message but it looks like it is returning every file in the directory with this warning above just modified for each file.
Here is the code that you gave me with my modifications:
Dont know if it matters but I put this code in between the body tags of the document
<?php
function searchfiles($q,$dir) {
//Add the extensions you want to search through here.
$exts = array('.php','.html','.htm','.docx','.doc','.pdf');
//Enter the files that you DON'T want to search through, eg. this page.
$exceptions = array('somefilethatyoudontwant',basename($_SERVER['PHP_SELF']));
//Sets the beginning of the search results and outputs it.
echo '<h2>Your Search Results</h2><ol>';
flush();
//Opens the directory
$handle = opendir($dir);
//Starts a loop through the directory to list the files
while ($file = readdir($handle)) {
//Check if the file is an exception
if (in_array($q,$exceptions)) {
continue;
}
//Check if it's showing '.' meaning this directory (the parent directory) or the directory above it (..)
else if ($file == '.' || $file == '..') {
continue;
}
//Loops through all the extensions
foreach ($exts as $extension) {
//Checks if it's an allowed file extension (eg. Word Documents, HTML Files etc.)
if (strpos($file,$extension)) {
//Gets the file contents...
$c = file_get_contents($dir.'\\'.$file);
//See if there's a match...
if (strpos($c,$q)) {
//Adds the URL
echo '<li><a href="'.$dir.''.$file.'" title="'.$file.'">'.getTitle($c,$file).'</a><br>';
//Gets the body content
echo getBody($c);
//Ends the list item
echo'</li>';
//Outputs it. This allows for the item to be displayed while the page is still loading. Good for when handling large files.
flush();
}
}
//Or else it'll continue through the loop.
else {
continue;
}
}
}
//And close the list.
echo '</ol>';
}
//Tool: stripPHP($c)
//Strips the PHP stuff for you so you can't search it.
function stripPHP($c) {
if (strpos($c,'<?php') != FALSE) {
$php = '';
$php = explode('<?php',$c);
$php[1] = explode('?>',$php[1]);
$c = str_replace($php[1][0],'',$c);
$c = str_replace('<?php?>','',$c);
return $c;
}
else {
return $c;
}
}
//Tool: chopParagraph($str)
//Chops the long content.
function chopParagraph($str) {
$words = 20;
$chopped = '';
$str = explode(' ',$str);
if (count($str)>30) {
for ($i=0; $i <= $words; $i++) {
$chopped.=$str[$i].' ';
}
return $chopped.'...';
}
else {
return $str[count($str)-1];
}
}
//Tool: getBody($content)
//Gets the body content.
function getBody($content) {
$content = stripPHP($content);
if (strpos($content,'<body>')) {
$content = explode('<body>',$content);
$content[1] = explode('</body>',$content[1]);
$returner = strip_tags($content[1][0]);
}
else {
$returner = filterWeirdStuff($content); //For Word Documents...
}
return chopParagraph($returner);
}
//Tool: getTitle($content,$filename)
//Gets the title of the page. If there's no title, it'll display the page name.
function getTitle($content,$filename) {
$content = stripPHP($content);
if (strpos($content,'<title>')) {
$content = explode('<title>',$content);
$content[1] = explode('</title>',$content[1]);
return htmlspecialchars($content[1][0]);
}
else {
return $filename;
}
}
//Tool: filterWeirdStuff($str)
//Filters the ???????????????????***3%^#$^4 you see when trying to get the file contents of Word Documents. Ugh.
function filterWeirdStuff($str) {
$str = explode(' ',$str);
unset($str[0]);
unset($str[1]);
$str = implode(' ',$str);
return '...'.$str;
}
if (isset($_POST['Submit'],$_POST['term'],$_GET['search'])) {
searchfiles($_POST['term'], 'ord');
}
else {
?>
<form action="?search=true" method="post">
<p>
<label>Search: <input type="text" name="term"></label>
</p>
<p>
<label><input type="submit" name="Submit" value="Search"></label>
</p>
</form>
<?php
}
?>
tech_support
07-27-2007, 07:00 AM
Aha. It's because you have spaces in the file name.
Change this line:
//Gets the file contents...
$c = file_get_contents($dir.'\\'.$file);
to this:
//Gets the file contents...
$c = file_get_contents($dir.'\\'.urlencode($file));
derekm
07-28-2007, 06:40 PM
Still getting warning messages as the results.
what do you mean spaces in the file name?
DM
thetestingsite
07-28-2007, 06:44 PM
what do you mean spaces in the file name?
Notice the spaces in this file name:
Microsoft Word - memorandum- recommended zoning ordinance.pdf
Not sure what the error could be from. Probably your best bet would be to do as I said before and make it a keyword search (which you would have to enter the keywords for the files). Other than that, no clue.
derekm
07-29-2007, 02:55 AM
would i need to change any of the coding for a key word search or just change it in the file itself
tech_support
07-29-2007, 10:38 AM
Well... did you change the directory?
Did you make sure the file exists in the folder?
would i need to change any of the coding for a key word search or just change it in the file itself
You'll need to change all of the coding.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.