Jump to content

Warning: strpos(): Empty needle in SearchScript.php on line 7


tobimichigan

Recommended Posts

Im having this error being thrown up on execution donno what could be wrong. Actually Im trying to display specific files as a url link for download from a search box. Could some1 tell me how I can get over this error?

<?php
$dir = 'dir for search'; 
$exclude = array('.','..','.htaccess'); 
$q = (isset($_GET['q']))? strtolower($_GET['q']) : ''; 
$res = opendir($dir); 
while(false!== ($file = readdir($res))) { 
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) { 
echo "<a href='$dir/$file'>$file</a>"; 
echo "<br>"; 
} 
} 
closedir($res); 
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>SEARCH SCRIPT</title>
</head>

<body>
<div id='header'>
SEARCH FOR FILES
</div>
<div id='formsearch'>
<form action='SearchScript.php' method='get'><input name='q'
type='text'> <input type='submit'></form>
</div>
<div>
</body>
</html>

the line 7 => 

 if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) { 

what is wrong with this line? Any ideas?

I think that the first time you execute the script, you have no value for $q. Try invoking the script with myscript.php?q=test to see if I'm right. Then change line 6 to

while(false!== ($file = readdir($res)) && $q!='') { 

One more thing David, on my local machine the dir is C:\xampp\htdocs\ASearch,

 

ie 

$dir = 'dir for search';

the code executes properly, but on my subdomain say http://s.hgf.com, where the target files are when I click the submit button, the browser just keeps loading and does not respond. Meanwhile the local 1 executes properly. How do you think I should link up to the subdomain in order for this script to work?

You'd need to setup $dir to be equal to whatever file-system (ie, not http: something) path you want to scan. If you do this with a relative path, or by creating a path using php's magic constants then you can probably use the same value both locally and remotely. Otherwise you'd need to check which server you're on using $_SERVER['HTTP_HOST'] or similar and set the variable accordingly

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.