File_exists()

I’m trying to displaty an image, but only if it exists. Other wise it’ll display some text.

if(file_exists('images/users/'.$row['avatar']))   {
echo '<img src="images/users/'.$row['avatar'].'" class="img-thumbnail" id="avatar" style="max-width:200px">';
} else {
echo "<h3 class='text-muted'>Upload Image</h3>";
}
?>

the result

<img src="images/users/" class="img-thumbnail" id="avatar" style="max-width:200px">

but the field in the table is empty (NULL)


But heres the directory

why is the image being shown?

file_exists — Checks whether a file or directory exists
http://php.net/manual/en/function.file-exists.php

The directory images/users/ exists, right?

Maybe add an if($row['avatar']) condition too.

From what I remember I had problems with file_exists. I use readfile instead.

I guess the name “file exists” is a bit misleading until one looks at the documentation. But then, something like “when path ends in file, file exists, when path ends in directory, directory exists” would be a mouthful of a name.

Using NULL values in the conditional would work, as would is_file and is_dir. Depends on whether you want to rely on the table value being the “authority”.

Also, if you plan on using id="avatar", and even if you don’t, having that in a loop is a bug waiting to happen.

1 Like

I believe the variable isn’t being passed around. That might be the reason why it outputs images/users/ instead of images/users/luke.jpg. You might want to enable error reporting for the entire project in a single config file. Then remove or comment the error reporting when transferring files from localhost to live server. I am pretty sure you’ll be getting Undefined Index if you enable errors.

Why not use error checking, etc?

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');

# CHECK IF ROW IS SET AND DISPALY VALUES 
  if( isset($row) ):
    echo '<pre>';
      print_r($row);
    echo '</pre>';
  else:
    echo '<hr> Yes we have no $row ??? <hr>';  
  endif;  

# CHECK and SET $img filename
  if( isset( $row['avatar'] ) ):
    // NO PROBLEM
    $img = 'images/users/' .$row['avatar'];
  else:  
   $img = './' .'corgi-on-stilts.jpg';
  endif; 

if(file_exists( $img ) )
{
  $src   = highlight_file( __file__, TRUE );
  $aDims = getimagesize($img);
  $sDims = print_r($aDims, true);

  $tmp = <<< ____TMP
    <img 
      src="$img" 
      class="img-thumbnail" 
      {$aDims[3]}
      id="avatar" 
      style="max-width:200px; height:auto;"
    >  
    <pre>
      $sDims
    </pre>  
    <dl style="width:88%; margin:2em auto;">
      <dt> Source file: </dt>
        <dd style="border:solid 1px #ddd; padding: 0.88em;"> $src </dd>
    </dl>  
____TMP;
echo $tmp;

} else {
  echo "<h3 class='text-muted'>Upload Image</h3>";
}
1 Like

The OP needs to clarify whether it works as expected when the table column says “luke.jpg”. Showing the image when it exists and the text when it doesn’t.

The OP says:-

So I made the assumption the problem only occurs when the value is null regardless of whether the file exists. In which case the reason is already explained, the file_exists function returns true if a directory exists, it is not specific to files only. Agreed it’s a misleading name for that reason.

Hmm, that is very misleading. If the variable isn’t being passed, then the main focus should be seeing why it’s not being passed. But if the issue is seeing whether someone has an avatar or not, all you need to do is check whether the column returns NULL. If it does, return a default avatar.

Yes, it would make sense to check if the value is null, which says the user has no avatar.
Though they may still want to check if the file exists when a file name is in the value, in case of any error. I suppose that depends on how well you trust the system to record file names and keep the files.

I do use file_exists in gallery scripts to avoid errors, blank frames, etc, in case of a typo, accidentally deleted/renamed image, forgotten to upload, etc…

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.