Jump to content

Am I Completely Missing Something Or Is This Being Awkward For Me


Recommended Posts

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 by drewwales

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!';
}

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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