View Full Version : PHP - Loading a jpg if a gif does not exist.


Pange
02-09-2003, 03:51 PM
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
$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\">"; }

?>

so basically, this script looks to see if the image.jpg exists. if it does, it shows the image on the page. if it doesn't, it will look for the same file name, but with a gif extension. I'm using dynamic links, so game = the directory where the image is stored, and image = the file in the directory to be loaded.

http://www.magitek.nu/ode2ff/layoutv2/gallery/gallery.php?image=main&game=ff9 tries to load this image: http://www.magitek.nu/ode2ff/layoutv2/gallery/ff9/main.jpg

http://www.magitek.nu/ode2ff/layoutv2/gallery/gallery.php?image=box&game=ff9 tries to load this image: http://www.magitek.nu/ode2ff/layoutv2/gallery/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 (http://www.magitek.nu/ode2ff/index2_1.php?ode=ff1_merch) 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! =)

pb&j
02-10-2003, 04:57 PM
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\">"; }

?>

Ökii
02-11-2003, 10:43 AM
due to only needing one method per if/else, you could use a tertiary there

$game = basename($game);
$image = basename($image);
echo '<img src="'.$game.'/'.$image.';
echo (file_exists($game.'/'.$image.'.jpg')) ? '.jpg" />' : '.gif" />';

if you still get snags, try echoing out the values of $game and $image to test they are set and accessible without going through a global array.

also - the {$game = "$game";} and img=$img lines should be removed rapidly

Pange
02-11-2003, 11:47 PM
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


69 <?php
70 $game = basename($game);
71 $image = basename($image);
72 echo '<img src="'.$game.'/'.$image.';
73 echo (file_exists($game.'/'.$image.'.jpg')) ? '.jpg" />' : '.gif" />';
74 ?>


So I have to add a "," or a ";" but I'm kind of confused about where to put the mark...you can definatly tell I'm a newbie with this. Do you have any advice?

Thanks again for your help! =)

pb&j
02-12-2003, 04:10 AM
Does this work?

$gameimage = basename($game)."/".basename($image);

if (file_exists($gameimage.".jpg")){
echo ('<img src=$gameimage.".jpg">');
}else{
echo ('<img src=$gameimage.".gif">');
}

Ökii
02-12-2003, 10:43 AM
oops

72 echo '<img src="'.$game.'/'.$image;

always tend to make the odd mistake when flytyping stuff for forums.

Pange
02-12-2003, 12:26 PM
Oh, it works perfectly! Thank you both for your help, I couldn't have done it without you :D

pb&j
02-12-2003, 01:49 PM
for the reference, which one worked? or both?
this may be handy on a future project.
thanks.

Pange
02-12-2003, 09:52 PM
It was actually Ökii's that worked =)

pb&j
02-12-2003, 11:25 PM
Cool, thanks.

Ökii
02-13-2003, 10:02 AM
the only part that would glitch on yours pb&j would be the echoing as variables are not parsed when placed in single quoted strings

$var = 'blat';
echo '$var' => $var
echo "$var" => blat

so
echo '<img src=$gameimage.".jpg">';
would ignore parsing the $gameimage var and just echo the string.

echo '<img src="' .$gameimage. '.jpg">';

is better.

notes: echoing single quoted strings is (in most cases) slightly faster as the parse engine doesn't have to check the string for variables. You do though, need to bounce out of the string to include vars eg '-----' .$var. '----';
I would advise starting to get the hang of single quoted strings as it will help you when you get to typecasting within class declarations.

tertiary (short if/else) operator

( a = b ) ? [do if true] : [do if false] ;
echo (7 > 4) ? 'true' : 'false'; => true
echo (7 < 4) ? 'true' : 'false'; => false

the first clause after the ? is assigned if the parenthesised expression equates to true, else the second clause is.
only works for single method clauses tho.

pb&j
02-13-2003, 12:30 PM
Originally posted by Ökii
the only part that would glitch on yours pb&j would be the echoing as variables are not parsed when placed in single quoted strings.
Dang, I knew (and forgot) about that too. Thanks.

Originally posted by Ökii
I would advise starting to get the hang of single quoted strings as it will help you when you get to typecasting within class declarations.
Yeah, I've kinda fallen into the pit of doubles and escapes.