Jim R Posted January 27, 2010 Share Posted January 27, 2010 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/ Share on other sites More sharing options...
teamatomic Posted January 27, 2010 Share Posted January 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002471 Share on other sites More sharing options...
Jim R Posted January 27, 2010 Author Share Posted January 27, 2010 Because I'm not sure how to do that either. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002472 Share on other sites More sharing options...
gwolgamott Posted January 27, 2010 Share Posted January 27, 2010 would something like that work??? $str = $file_name; $bob = (explode(".",$str)); if($bob[1] == ".jpg") {do something} if($bob[1] == ".gif") { do something} Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002474 Share on other sites More sharing options...
gwolgamott Posted January 27, 2010 Share Posted January 27, 2010 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002475 Share on other sites More sharing options...
teamatomic Posted January 27, 2010 Share Posted January 27, 2010 list($file_name,$ext)=explode(".",$file); if ($ext == 'JPG'){ $new_file=strtolower("$file"); rename("$file",$new_file"); } HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002481 Share on other sites More sharing options...
gwolgamott Posted January 27, 2010 Share Posted January 27, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002484 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002836 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 You'd have to echo $file_name in your img src tag. <div><img src="<?php echo $file_name; ?>" /></div> Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002869 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002881 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002883 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 Still not working, same result. Doesn't see the .jpg file, and if there is a .JPG file it echo's that. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002884 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 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! =) Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002887 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 Not working. When I click on the image properties of the one not showing, it shows a file format of .JPG. That's supposed to be a .jpg one, so it's not recognizing that there is a .jpg file there and echo'ing a .JPG. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002890 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 I might have misunderstood you. When you say it's echoing a .JPG, do you mean it's just showing that as the file extension or can you actually see the image? If you can't see the image then it's just a path error in the script. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002891 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 No, the path is otherwise fine. It's just showing the .JPG part, which in this case doesn't exist, when the .jpg file exists. That's why I keep saying, the code as we have it bypasses all .jpg line and just uses the .JPG line. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002892 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002893 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 Not only does it not work, it's not even the right path. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002897 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 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']; Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002898 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 The last version didn't even drill back to the root folder, so it's just adding the image URL to the end of the page URL I'm looking at. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002899 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 Just to help me understand the problem, could you post path examples of the script and images? Something like: http://mydomain.com/thescript.php http://mydomain.com/wp_uploads/images/etc.jpg Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002901 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 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/ Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002902 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 Could you post an example path of where the .php script resides? Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002903 Share on other sites More sharing options...
Jim R Posted January 28, 2010 Author Share Posted January 28, 2010 Not sure what you mean, but the file that actually produces that code within my Wordpress is: http://hoosierhoopsreport.com/resources/wp-playerProfile.php Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002904 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/190005-i-need-some-help-with-the-if-command/#findComment-1002905 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.