PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$fn = "/images/avatar/<?=$imgfile?>.gif";
if(file_exists($fn)) {
//Nothing
} else {
//file does not exist
$imgfile = "noimg";
}
It returns else every time, Im sure something about that path isnt right. Can someone a little more experienced please check if there is anything else I need to put before that path.
You don't want to echo - i.e. print, output, send to the client - the contents of the variable. You want a variable substitution, e.g. imgfile=abc -> $fn = /images/avatar/abc.gif
And you're already within a php block. You cannot open another. <?= is the short form of <?php echo
Okay thanks for this help, obviously im not doing it right, please can you show me an example of what you mean?
Could this be what is wrong with my code?
tommy1987 wrote:Okay thanks for this help, obviously im not doing it right, please can you show me an example of what you mean?
Could this be what is wrong with my code?
And JayBird deleted one of his posts that might also be helpful.
It was about the path /images/avatar/. The leading / makes it an absolute path. An absolute path in the local filesystem and that's usually not the same as the document root of the webserver, i.e. if http://localhost/images/avatar/abc.gif works it doesn't mean /images/avatar/abc.gif must work for file_exists, usually it doesn't.
Where (in the local filesystem) is the script file located? Where is that directory /images ?
<?php
$fn = "/images/avatar/<?=$imgfile?>.gif";
if(file_exists($fn)) {
//Nothing
} else {
//file does not exist
$imgfile = "noimg";
}
?>
Look at your code. You see the opening and closing PHP tags at the beginning and end of the snippet? If you are already in PHP, you can't open PHP again. All you need to do is utilize the variable within the string. Much like this...
<?php
$mystring = 'This is a string';
echo 'My script says ' . $mystring;
// Or to parse it directly in the string...
echo "Now my script says $mystring";
?>
<?php
$fn = "/images/avatar/$imgfile.gif";
// Or like this, but use just one of them
$fn = '/images/avatar/' . $imgfile . '.gif';
if(file_exists($fn)) {
//Nothing
} else {
//file does not exist
$imgfile = "noimg";
}
?>