Page 1 of 1

file_exists problem

Posted: Tue Oct 24, 2006 6:36 am
by tommy1987
Hi here is my code:

Code: Select all

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

Thanks for help.

Posted: Tue Oct 24, 2006 6:52 am
by volka
$fn = "/images/avatar/<?=$imgfile?>.gif";
What's that supposed to do?
You probably want

Code: Select all

$fn = "/images/avatar/{$imgfile}.gif";
If imgfile is a POST/GET parameter e.g. from a link or a form, please read http://de2.php.net/security.globals

Posted: Tue Oct 24, 2006 7:00 am
by tommy1987
no that isnt the problem, $imgfile is set earlier as you can see here;

Code: Select all

$imgfile = strtolower($row['user']);  
			$fn = "/images/avatar/<?=$imgfile?>.gif";
			if(file_exists($fn)) {
					
			} else {
				//file does not exist
				$imgfile = "noimg";	
			}
      echo '<div class="cascade">';
      ?>
    			<img class="smallav" src="images/avatar/<?=$imgfile?>.gif" alt="" />
      <?php
The problem lies with the fact that it isnt picking up the file when it exists, i think it is something to do with the path, although not sure.

Posted: Tue Oct 24, 2006 7:10 am
by volka
But why <?= ?
Do you have files called <?=pic001?>.gif ?
I doubt it.

Posted: Tue Oct 24, 2006 7:46 am
by tommy1987
no but that echoes in the filename, and then appends the '.gif' afterwards. What is the problem with that?
<?= simply performs an echo

Posted: Tue Oct 24, 2006 7:52 am
by volka
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

Posted: Tue Oct 24, 2006 7:54 am
by tommy1987
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?

Posted: Tue Oct 24, 2006 8:09 am
by JayBird
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?
He has already shown you in his first post

Posted: Tue Oct 24, 2006 8:14 am
by volka
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 ?

Posted: Tue Oct 24, 2006 9:47 am
by tommy1987
I cant seem to understand that, it works that way I have done, yet the way you suggest makes it no longer work..

Re: file_exists problem

Posted: Tue Oct 24, 2006 10:34 am
by RobertGonzalez
tommy1987 wrote:

Code: Select all

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

Code: Select all

<?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";
?>
So in your case it would be like this...

Code: Select all

<?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";	
}
?>