tobimichigan Posted March 9, 2014 Share Posted March 9, 2014 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? Quote Link to comment Share on other sites More sharing options...
Solution davidannis Posted March 10, 2014 Solution Share Posted March 10, 2014 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!='') { Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted March 10, 2014 Author Share Posted March 10, 2014 briliant David, I did change line six from while(false!== ($file = readdir($res))) { to while(false!== ($file = readdir($res)) && $q!='') { ..and it stopped the error. Thanks a mil. and keep the skill up Pal. Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted March 10, 2014 Author Share Posted March 10, 2014 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? Quote Link to comment Share on other sites More sharing options...
kicken Posted March 11, 2014 Share Posted March 11, 2014 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 Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted March 11, 2014 Author Share Posted March 11, 2014 you mean like echo $_SERVER['HTTP_HOST']? hmm? 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.