If file exists show image uploaded else show no image file

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
admin2gd1
Forum Newbie
Posts: 15
Joined: Fri Aug 15, 2008 2:21 pm

If file exists show image uploaded else show no image file

Post by admin2gd1 »

Here is something quick someone can help me with. I can't get the second image to show.

If the photo in the database and directory doesn't exist then show image 2 but it's not doing it because it is still trying to read from the directory where the photos would be, so it ignores the second image in less that directory does not exist also.

Now the thing is I have 12 photos all together in one directory and the directory needs to exist for the other 11 photos so what I want is the second image to show and not try to read from the directory.

This is my code that I am using, don't worry about $SITEurl and $username and $name they are working.

Code: Select all

<?php if(file_exists("../photos/properties/$username/$name/$photo1")){ ?>
<img class="photo" src="<?php echo $SITEurl ?>/photos/properties/<?php echo $username; ?>/<?php echo $name; ?>/<?php echo $photo1; ?>" alt="">
<?php }else{ ?><img class="photo" src="<?php echo $SITEurl; ?>/images/<?php echo getLang(noimage); ?>" alt=""><?php } ?>
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: If file exists show image uploaded else show no image file

Post by Sindarin »

I don't see anything that would tell the script (while loop) to list the whole folder,

I use this for my cms wherever it needs to check and show an image inside a while loop to get all the entries,
It gets the path from database and tries to find the image,
if it does it displays it.
If it doesn't find an entry it ouputs "without image" and if it doesn't find the file
it outputs an option to clean up the false entry.

Code: Select all

 
 
while($row = mysql_fetch_array($db_fetch))
  {
  $newsid=$row['news_id'];
  $newsimage=$row['news_thumbnail'];//Check for images
if ($newsimage=="")
        {
        echo "Without image";
        }
        else
        {
        $file_check="$newsimage";
        if (file_exists($file_check))
        {
        echo "<img src='$newsimage' border=0 alt='Image' />";
        }
        else
        {
        echo "<font color='red'><img src='files/images/layout/cms-alert.png' border=0 alt='Warning' /> Image has been deleted from the server but entry in the database exists! </font>[ <a href='show.php?action=cleanupimage&imagesrc=$newsimage'> CleanUp </a> ]";
        }
        }
 
}
Last edited by Sindarin on Wed Sep 03, 2008 12:41 pm, edited 2 times in total.
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: If file exists show image uploaded else show no image file

Post by starram »

Try using this, if this doesn't work then try to give absolute path in the file_exists functions. If that doesn't work too try to give document root path like this:
Note this is just an example: user/html/httpdocs/photos/properties

Code: Select all

<?php 
    if(file_exists("../photos/properties/$username/$name/$photo1"))
    { 
?>
        <img class="photo" src="<?=$SITEurl?>/photos/properties/<?=$username?>/<?=$name?>/<?=$photo1?>" alt="" />
<?php 
    }else{ 
?>
    <img class="photo" src="<?=$SITEurl?>/images/<?=getLang(noimage)?>" alt="" />
<?php   
    } 
?>
Post Reply