file_exists problem

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!

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

file_exists problem

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Last edited by volka on Tue Oct 24, 2006 6:52 am, edited 1 time in total.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

But why <?= ?
Do you have files called <?=pic001?>.gif ?
I doubt it.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ?
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post 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..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: file_exists problem

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