We help IT Professionals succeed at work.

php file_exists

pda4me
pda4me asked
on
can someone show me via and example code of file_exists?

Basically I want to check and see if a .jpg file exists in directly c:\images\ and if so it displays this dynamic a href or if not it display c:\images\noPhoto.jpg

<a href="http://www.domain.com/images/<?php echo($row_WADA['NUMBER']); ?>.JPG" rel="thumbnail"><img src="http://www.domain.com/images/<?php echo($row_WADA['NUMBER']); ?>.JPG" width="240" height="300" />            </a>  

Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016

Commented:
This would be an if / else construct.  What variable has the file path?
Here you go :)
<?php
$path="C:\images";
// will need to set $filename yourself
if (file_exists($path."\".$filename)){
?>
<a href="http://www.domain.com/images/<?php echo($row_WADA['NUMBER']); ?>.jpg" rel="thumbnail"><img src="http://www.domain.com/images/<?php echo($row_WADA['NUMBER']); ?>.jpg" width="240" height="300" />            </a>  
?<php
}
else
{
    print "<img src="C:\images\noPhoto.jpg" />";
}
// You should try and use relative paths instead of absolute paths, if you ever move your site from C:\ etc then you will have to update your code accordingly
Most Valuable Expert 2011
Top Expert 2016
Commented:
Obviously untested code, but this is the concept... HTH, ~Ray
if (file_exists('c:' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $image_file_name))
{
    /* ECHO LINK TO IMAGE HERE */
}
else
{
    /* ECHO LINK TO NO-IMAGE-EXISTS THING HERE */
}

Open in new window

Vimal DMSenior Software Engineer
Commented:
Hai,

You can use the built in function
file_exists("test.txt"); ,which will return 0 or 1 means exists or not,
so make your if condition here,
-------------------------------------------
if(file_exists("c:\images\test.jpg"))
   $path = c:\images\test.jpg;
else
   $path = c:\images\noimage.jpg

<img src="<?=$path?>" width="240" height="300" />


Thanks
Avinash ZalaWeb Expert
Commented:
First of all let me say you something.

File existence should be checked using document path and displaying of image should be done by webpath.

So your code should be like below..

try this code:::

This will surely helps.
<?php
	
	$doc_path = $_SERVER['DOCUMENT_ROOT'].'images/';
	$webPath = 'http://'.$_SERVER['HTTP_HOST'].'/images/';	
	
//	considering you will get dynamic image name in $imageName variable

	if(file_exists($doc_path.$imageName))
	{
		$img=$imageName;
	}
	else
	{
		$img ='noImage.jpg';
	}

?>
<img src="<?php echo $webPath.$img;?>" />

Open in new window

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You do not have to use the server path, you can check files with a relative path in the web root just fine.  I had to use this code this weekend on an active website to avoid showing a bunch of 'alt' tags.  This may not work properly on 1and1 hosting because they don't give proper error messages.
if(file_exists($stdpic)) {
  >> do something
  }
else {
  >> something else
  }

Open in new window

Author

Commented:
Thanks!