Jump to content

I need some help with the IF command...


Jim R

Recommended Posts

I have a large number of images in one section of my website with the .jpg format.  I'm adding a bunch of new images thanks to a new camera, but in using Picasa to bulk export smaller images sizes, they are all exported with .JPG.

 

These are head shots (mug shots) for basketball player profile pages.  It's just supposed to be one photo, but the .jpg code isn't printing the .JPG files, which is obvious, so I added a line to print the newly exported .JPG files.  The problem is I'm getting two items printed, the image and an image holder.

 

How do I code it to basically say.... "if *this* file type exists....echo *this* file....else if *this* file type exists....echo *this*.

 

Here is the code I'm using:

 

echo '<div><img src="/wp-content/uploads/' . $line['nameLast'] . $line['nameFirst'] . '.jpg"></div>';
echo '<div><img src="/wp-content/uploads/' . $line['nameLast'] . $line['nameFirst'] . '.JPG"></div>';

 

 

Link to comment
https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/
Share on other sites

  • Replies 54
  • Created
  • Last Reply

Why don't you leave it as original and just  explode the file name and if the extension matches JPG rename the file and move happily along? Better yet on the display page include a file that traverses the image folder renaming upper case extensions to lowercase?

 

 

HTH

Teamatomic

 

 

another way of doing it perhaps

 

$file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (file_exists($file_name)) 
{
//Do nothing
}
else
{
$file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".JPG";
}

$file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (!file_exists($file_name)) $file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".JPG";

Should have streamlined that. Sorry there is a more efficient version if you want a simple check method.

Gwolgamott, neither of those work, and just looking at it there is no echo codes.  I added the echo code, and they still don't work.  They just echo the .JPG file.

 

 

The suggestions utilizing the explode code are conceptually over my head.  I don't know how to apply them.

This is what I'm using now, and if it's a .jpg file, it's not printing at all, just shows an image placeholder.  If it's a .JPG file it's echo'ing the image.

 

$file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (file_exists($file_name)) 
{ echo '<div><img src="<'. $file_name .'"></div>';
//Do nothing
}
else
{
echo '<div><img src="/wp-content/uploads/' . $line['nameLast'] . $line['nameFirst'] . '.JPG"></div>';
}

Try:

	

$file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (file_exists($file_name)) { 
echo '<div><img src="'. $file_name .'"></div>';
}
else {
echo '<div><img src="/wp-content/uploads/' . $line['nameLast'] . $line['nameFirst'] . '.JPG"></div>';
}

 

You had an extra bracket in the img tag. ;)

If you right click and view the image you should be able to see why your browser isn't able to find the image.

 

Gwolgamott's code should work fine with an echo added anyway. :)

 

<?php
$file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (!file_exists($file_name)) $file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".JPG";
?>
<div><img src="<?php echo $file_name; ?>" /></div>

 

Or:

$file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (!file_exists($file_name)) $file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".JPG";
echo "<div><img src=\"$file_name\" /></div>";

 

Whichever takes your fancy! =)

Can you try:

 

$file_name = "wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (!file_exists($file_name)) $file_name = "wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".JPG";
echo "<div><img src=\"$file_name\" /></div>";

 

That ought to work! ;)

I recreated everything on my side and tested it out the one I posted; it worked fine.

 

I'm not sure what's going wrong for you other than your path is set wrong, sorry! :-[

 

Perhaps your path needs to be something along the lines of:

/home/username/public_html/wp-content/uploads/image.jpg

 

You can get your full path by doing:

echo $_SERVER['DOCUMENT_ROOT'];

OK...here is the code I'm currently using, which doesn't work:

 

$file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (!file_exists($file_name)) $file_name = "/wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".JPG";
echo '<div><img src="' . $file_name .'" /></div>';

 

 

Here is an example where the image file is a .JPG:

http://hoosierhoopsreport.com/tag/alexander-hutson/

 

Here is an example where the image file is a .jpg:  (image holder is blue box just left of name)

http://hoosierhoopsreport.com/tag/deshaun-thomas/

 

 

Can you try:

$file_name = "../wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".jpg";
if (!file_exists($file_name)) $file_name = "../wp-content/uploads/". $line['nameLast'] . $line['nameFirst'] . ".JPG";
echo '<div><img src="' . $file_name .'" /></div>';

 

Fingers crossed. ;)

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.