drewwales Posted December 21, 2012 Share Posted December 21, 2012 (edited) this works and displays images if found within the specified folder. but if the image is not found it does bugger all instead of displaying and echo saying no file. Heres the code; if ($handle = opendir("../images/services/")) { while (false !== ($file = readdir($handle))) { $strFielName = ""; $strFielName.= basename($file, ".jpg"); if ($strFielName == $strPageTitle) { if (($file != "." && $file != ".." && $file != "/." && $file != "/..") && (preg_match('/\.(jpg)([^\.]*$)/', $file, $extension))) { $myfile = "../images/services/".$file; if (file_exists($myfile)) { echo "$myfile"; echo "<center><div style='width:220px;'><a href=\"../images/services/".$file."\"><img src=\"../images/services/".$file."\" width=\"200\" height=\"178\"></a><br>"; echo "<form method=\"post\" name=\"deleteSomething\" action="?><?php echo $_SERVER['PHP_SELF'].'?delete=true';?><?php echo" > <input type=\"hidden\" name=\"fileToDelete\" value=".$file." > <input type=\"submit\" value=\"Delete\"></form></div></center>"; } else { echo "no file"; } } } } closedir($handle); } Edited December 21, 2012 by drewwales Quote Link to comment https://forums.phpfreaks.com/topic/272251-am-i-completely-missing-something-or-is-this-being-awkward-for-me/ Share on other sites More sharing options...
SofWare Posted December 21, 2012 Share Posted December 21, 2012 (edited) Php is being awkward for you :-) Or perhaps one of the previous if statements is false (so the php does not reach the file_exists if statement)? Edited December 21, 2012 by SofWare Quote Link to comment https://forums.phpfreaks.com/topic/272251-am-i-completely-missing-something-or-is-this-being-awkward-for-me/#findComment-1400797 Share on other sites More sharing options...
jcbones Posted December 21, 2012 Share Posted December 21, 2012 You could use glob, and it will only pull files of a specified type. EX. $path = '../images/services/'; $image = glob($path . '*.jpg'); //only files that end in jpg if(is_array($image)) { foreach ($image as $filename) { echo $filename . '<br />'; } } else { echo 'No jpg images!'; } Quote Link to comment https://forums.phpfreaks.com/topic/272251-am-i-completely-missing-something-or-is-this-being-awkward-for-me/#findComment-1400800 Share on other sites More sharing options...
DavidAM Posted December 21, 2012 Share Posted December 21, 2012 Why would you loop through an entire directory to look for one file when you know the name of the file you want? $imageDir = "../images/services/"; $pageFile = $strPageTitle . ".jpg"; if (file_exists($imageDir . $pageFile) { // Do your image output here } else { // Do your NO IMAGE output here } Or did I miss something? Quote Link to comment https://forums.phpfreaks.com/topic/272251-am-i-completely-missing-something-or-is-this-being-awkward-for-me/#findComment-1400829 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.