![]() |
|
#1
|
||||
|
||||
|
PHP - Loading a jpg if a gif does not exist.
Hey! I'm fairly new to writing PHP, but I've handled a few small scripts on my website. I wrote this script, but it doesn't seem to work the way I want it to.
PHP Code:
http://www.magitek.nu/ode2ff/layoutv...=main&game=ff9 tries to load this image: http://www.magitek.nu/ode2ff/layoutv...y/ff9/main.jpg http://www.magitek.nu/ode2ff/layoutv...e=box&game=ff9 tries to load this image: http://www.magitek.nu/ode2ff/layoutv...ry/ff9/box.gif for some reason, it is able to work for gif files, but any jpgs do not work. It's not a problem with that one jpg file, because I tried it here as well and it does not work... if anyone has any information on this to help, I will be very greatful. This is driving me insane, lol. thanks! =) |
|
#2
|
||||
|
||||
|
Should the periods be escaped in the filenames?
<?php $game = basename($game); { $game="$game"; } $image = basename($image); { $image="$image"; } if(file_exists("$image\.jpg")) { $image = "$image\.jpg"; echo "<img src=\"$game/$image\">"; } else { $image = "$image\.gif"; echo "<img src=\"$game/$image\">"; } ?>
__________________
phantom of the forum! mwa-ha-ha!
|
|
#3
|
||||
|
||||
|
due to only needing one method per if/else, you could use a tertiary there
PHP Code:
also - the {$game = "$game";} and img=$img lines should be removed rapidly |
|
#4
|
||||
|
||||
|
thanks for the replies, you two!
I tried pb&j's code first, and when I escaped the periods, the image properties said the file was main\.jpg. Thanks for trying anyways though ![]() Okii, thanks for yours! I tried it, but I get an error, but I know it's picking up the $game and $image variables by echoing them. This is the error message I get: Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/.hassansybian/magitek/magitek.nu/ode2ff/layoutv2/gallery/gallery.php on line 72 PHP Code:
Thanks again for your help! =) |
|
#5
|
||||
|
||||
|
Does this work?
$gameimage = basename($game)."/".basename($image); if (file_exists($gameimage.".jpg")){ echo ('<img src=$gameimage.".jpg">'); }else{ echo ('<img src=$gameimage.".gif">'); }
__________________
phantom of the forum! mwa-ha-ha!
|
|
#6
|
||||
|
||||
|
oops
72 echo '<img src="'.$game.'/'.$image; always tend to make the odd mistake when flytyping stuff for forums. |
|
#7
|
||||
|
||||
|
Oh, it works perfectly! Thank you both for your help, I couldn't have done it without you
|
|
#8
|
||||
|
||||
|
for the reference, which one worked? or both?
this may be handy on a future project. thanks. |
|
#9
|
||||
|
||||
|
It was actually Ökii's that worked =)
|
|
#10
|
||||
|
||||
|
Cool, thanks.
__________________
phantom of the forum! mwa-ha-ha!
|
| Thread Tools | |
| Display Modes | |
|
|