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
Printable View
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
Might run a bit slow. Using a database with a precompiled list of words might be beneficial.
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.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;
}
print_r(searchfiles($search,'mydocs')); //set mydocs to your directory
//$search must be the text to match
?>
Thank You
I will try it and see
where would I put the search box and the submit button in this script?
You would want to do something like this:
Hope this helps.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'], '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
}
?>
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
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.
Thank You for your time
DM
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
You took out the slash.
$dir.'/'.$file --> dir/file.ext
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
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.
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.
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
Ok here is what I have:
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:Quote:
<?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
}
?>
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
Sorry about that, forgot to add a closing quote after the form action like so:
That should fix itCode:<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
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
Quote:
<?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
}
?>
Not sure if this will work or not, but you could try this:
Changes made are in red. Hope this helps.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;
$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
}
?>
<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.
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
And Line 69 is this:Quote:
Warning: strpos(): Empty delimiter. in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
And just for further refreshing here is the whole code again:Quote:
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
And you said earlier that something about assuming $dir and $file are set correctly, where was i susposed to set that at :)Quote:
<?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
}
?>
Thanks
DM
so your saying I have it right?
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.
well that is the exact directory and it is not working for some reason...
what does "Empty delimiter" mean?
With this php code
I am getting no form at all and getting this on my page..Quote:
<?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
}
?>
LINE 69Quote:
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 ideas.....Quote:
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
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?
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
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 searchingQuote:
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
Any Suggestions or am I totally screwedQuote:
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
DM
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;
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
Quote:
<?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
}
?>
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);
Does the above code need to be exactly like it looks or on one lineQuote:
$fc = file_get_contents($dir.'/'.$file)
if ($fc == '') continue;
if (!strpos($search,$fc)) continue;
That's right. But again, it's missing a semi colon. Add that to the first line, like in my last post.
djr33, you're missing one key point.
Quote:
Originally Posted by Original Question
What do you mean tech_support