PHP: How to display a default Image if the specified one doesn't exists?
Solution 1
There are many ways to do this.
<?php
if(file_exists("images/artists/$artist_name.jpg"))
$fileName = "$artist_name.jpg";
else
$fileName = "default.jpg";
?>
<div class="artimg"><img src="images/artists/<?php echo $fileName;?>" height="50px" width="50px"/></div>
Solution 2
Disk Access = slow
You have a couple of options. One is to check that the file exists each and every time you need to display it. Unfortunately, this is less than ideal because disk reads can be a performance bottleneck. It's not a tenable solution if you're serving up hundreds of page loads a minute. In high-performance environments you want to avoid as many file stats as possible:
$img_dir = '/path/to/artist/images';
$expected_file = $img_dir . '/' . $artist_name . '.jpg';
$img = file_exists($expected_file) ? $artist_name : 'default';
Hard-code the names into your code
Another, if you have a small number of artists whose images you need to display is to hard-code in the names for the images you have:
$authors = array('Bill Shakespeare', 'Douglas Adams', 'Ralph Ellison');
$img = in_array($artist_name, $authors) ? $artist_name : 'default';
Of course, this isn't particularly helpful because then you'll need to edit your code each time your catalog of artist images changes.
The best idea ...
The preferred option in this instance would be this:
Since you're already hitting the database to get the artist records, store the relevant image filename in a database column of the artists table. That way you can avoid the superfluous disk access.
This method allows you to retrieve the filename from the database with your original query. If the field is empty you'll know to display the default image instead.
Roger Williams
Updated on June 04, 2022Comments
-
Roger Williams almost 2 years
I am working on a little project and I need to display the author's image for each author in my database. peep the code below:
--THE QUERY--
$getboth_sql = mysql_query( "SELECT lyrics.lyrics_id, lyrics.lyrics_title, lyrics.lyrics_text,artists.artist_nane,artists.artist_id FROM lyrics,artists WHERE lyrics.artist_id = artists.artist_id ORDER BY lyrics.lyrics_title"); while ($both = mysql_fetch_assoc($getboth_sql)) { $lyrics_id = $both[lyrics_id]; $lyrics_title = $both[lyrics_title]; $lyrics_text = $both[lyrics_text]; $artist_name = $both[artist_nane]; $artist_id = $both[artist_id]; ?> <div class="artimg"><img src="images/artists/<?php echo $artist_name;?>.jpg" height="50px" width="50px"/></div> <?php }//END While
The above code work but if I have not saved an image in the 'artists' directory no image show up for that artist.
<div class="artimg"><img src="images/artists/<?php echo $artist_name;?>.jpg" height="50px" width="50px"/></div>
QUESTION: What is the BEST way to display a default image if the specified on is not found.
I have been playing around with the empty and file exists functions but i haven't gotten it right. Please Help.
PS I'm not a pro if this question seems stupid!
-
jprofitt about 12 yearsGoing to be doubling up the ".jpg" by adding it in the
<img>
as well. -
Roger Williams about 12 yearsThanks for the tip but for some reason I keep getting the default.jpg for all the artist, even if there name in the folder. :(
-
Rahul Mandaliya over 9 years@RogerWilliams For your kind of information please check here, this file_exists method has major problem. function file_exists ($filename) {} /** * (PHP 4, PHP 5)<br/> * Tells whether the filename is writable * link php.net/manual/en/function.is-writable.php * param string $filename <p> * The filename being checked. * </p> * return bool true if the filename exists and is * writable. */ as you read here file_exists methods return TRUE only if file is found and its WRITABLE otherwise its not usable. I tried this method and get FALSE every time if file is contain in folder.
-
devlin carnate about 8 yearsYour question could be improved if you added an explanation for why it resolves the problem. A line of code with no explanation is generally considered to be a poor quality answer.