maxwel Posted May 23, 2013 Share Posted May 23, 2013 <?php $dir = 'direction'; $q = (isset($_GET['q']))? strtolower($_GET['q']) : ''; $res = opendir($dir); f (strlen($q) < 3) { echo "Search must be longer than 3 characters.<br><br><a href='http://mbrservices.no-ip.org/search.html'>Back To Search Box</a>"; } else { if (!( ($q == "mp3") || ($q == "wav") || ($q == ""))){ while(false!== ($file = readdir($res))) { if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) { $nicefile = str_replace(".mp3", "", "$file"); $info = pathinfo($file); if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) { show links of the files that contain query of the string) the problem is that i want to create an if statment which check if there is files of query that user searhed show it else if the search result is ended up with nothing it return an error msg. i tried alot of things like: if ($files != ""){show the files} elseif(!isset($errorMsg)){errormsg} if (!empty($files)){show the files} elseif(!isset($errorMsg)){errormsg} if ($files != 0){show the files} elseif(!isset($errorMsg)){errormsg} and more but nothing ended up right they always gave blank with no error msg or nothing, any idea? Thanks alot, Maxwel Quote Link to comment https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/ Share on other sites More sharing options...
xenLiam Posted May 24, 2013 Share Posted May 24, 2013 Is $files an array? If that's the case then you can use this: if(count($files) != 0) { // Show the files } else { // Show the error message } I can't seem to understand your code, it's broken. I don't know where $files came from. Including the part where $files is declared can help. Quote Link to comment https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/#findComment-1431996 Share on other sites More sharing options...
Q695 Posted May 24, 2013 Share Posted May 24, 2013 are you uploading multiple files, if so do it with something like a while ($_FILE['variable_name']){.....} to pull out each of the files being submitted by the same name. Quote Link to comment https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/#findComment-1432011 Share on other sites More sharing options...
maxwel Posted May 24, 2013 Author Share Posted May 24, 2013 (edited) its not uploading its like searching directory and coming up with results that contain such query. <?php $dir = 'dir'; $exclude = array('.','..','.htaccess'); $q = (isset($_GET['q']))? strtolower($_GET['q']) : ''; $res = opendir($dir); if (strlen($q) < 3) { echo "Search must be longer than 3 characters.<br><br>"; } else { if (!( ($q == "mp3") || ($q == "wav") || ($q == ""))){ while(false!== ($file = readdir($res))) { if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) { $nicefile = str_replace(".mp3", "", "$file"); $info = pathinfo($file); if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) { if(count($file) != 0) { echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>"; echo "<br>"; }elseif(!isset($errorMsge)){ $errorMsge = "ERROR: File not found.<br><br>"; }else{} }elseif(!isset($errorMsg)){ $errorMsg = "ERROR: File not found.<br><br>"; }else{} } } if (isset($errorMsge)){echo $errorMsge;} else{} if (isset($errorMsg)){echo $errorMsg;} else{} }else{echo"ERROR: File not found.<br><br>";} } closedir($res); ?> this is the full code and the problem is that iwhen i try to search something that does not exist in directory it dont give error msg but it gives nothing in return :S Edited May 24, 2013 by maxwel Quote Link to comment https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/#findComment-1432036 Share on other sites More sharing options...
Eiseth Posted May 24, 2013 Share Posted May 24, 2013 Be sure that your error reporting is on, and if you look at your code you have lots of else{} that doesn't do anything. Try putting echo at those and see if it outputs Quote Link to comment https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/#findComment-1432042 Share on other sites More sharing options...
maxwel Posted May 24, 2013 Author Share Posted May 24, 2013 i found that when i do it like this <?php $dir = 'dir'; $exclude = array('.','..','.htaccess'); $q = (isset($_GET['q']))? strtolower($_GET['q']) : ''; $res = opendir($dir); if (strlen($q) < 3) { echo "Search must be longer than 3 characters.<br><br>"; } else { if (!( ($q == "mp3") || ($q == "wav") || ($q == ""))){ while(false!== ($file = readdir($res))) { if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) { $nicefile = str_replace(".mp3", "", "$file"); $info = pathinfo($file); if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) { echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>"; echo "<br>"; }elseif(!isset($errorMsg)){ $errorMsg = "ERROR: File not found.<br><br>"; }else{} }elseif(!isset($errorMsge)){ $errorMsge = "ERROR: File not found.<br><br>"; }else{} } if (isset($errorMsge)){echo $errorMsge;} else{} if (isset($errorMsg)){echo $errorMsg;} else{} }else{echo"ERROR: File not found.<br><br>";} } closedir($res); ?> it shows the error msg whether there is results or not.seriously, i dont know why it shows if in if condition got no error msgs that i added Quote Link to comment https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/#findComment-1432043 Share on other sites More sharing options...
Solution DavidAM Posted May 24, 2013 Solution Share Posted May 24, 2013 You are making this more difficult than it needs to be (I should know, I do that all the time). You can't say "File not found" inside the while loop just because the current file does not match. You have to make a note when you DO find a file and then echo the "Not Found" message AFTER the loop (if you didn't find one). Try this rewrite: $dir = 'dir'; $exclude = array('.','..','.htaccess'); $q = (isset($_GET['q']))? strtolower($_GET['q']) : ''; $res = opendir($dir); if (strlen($q) < 3) { echo "Search must be longer than 3 characters.<br><br>"; } elseif (($q == "mp3") || ($q == "wav")) { echo "Invalid Search Criteria<BR>"; } else { $foundOne = false; while(false!== ($file = readdir($res))) { if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) { $info = pathinfo($file); $nicefile = $info['filename']; if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) { echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>"; echo "<br>"; $foundOne = true; } } } if (!$foundOne) echo "No files found<BR>"; } closedir($res); I don't know if it was the forum that messed up your code, or you just didn't use indenting. If it was you, learn to use it, it makes it clearer as to what is part of an IF or ELSE or LOOP or whatever. Quote Link to comment https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/#findComment-1432097 Share on other sites More sharing options...
maxwel Posted May 24, 2013 Author Share Posted May 24, 2013 thanks alot, worked well so much appreciated Maxwel Quote Link to comment https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/#findComment-1432131 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.