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
Share on other sites

  • Replies 54
  • Created
  • Last Reply

Top Posters In This Topic

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

 

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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! =)

Link to comment
Share on other sites

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! ;)

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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/

 

 

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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.