ltdev
ltdev

Reputation: 4477

Check if the image and file type exist inside directory

This is my code:

$file = 'images/img_' . $post->_id . '.jpg'; // images/img_1.jpg
    if (file_exists($file) && is_file($file)) {
                //show the image
                echo '<img src="' . $file . '" class="img-responsive" />';
            } 
            else {
                // show default image 
                echo '<img src="images/default.gif" class="img-responsive" />'; 
            }

What this basicly does is to check if the an image (eg img_1.jpg) exist in my images folder and display it, if not it shows a default image.

This works fine, the only problem, as you probably 've seen, is that it checks only the .jpg files and not *.gif or *.png too (the file could also be img_1.gif or img_1.png, right? ) .

How can I make sure that it will check against all valid file types too and show the file with the correct format?

Upvotes: 0

Views: 1846

Answers (3)

Joe Meyer
Joe Meyer

Reputation: 4405

The most efficient way to do this is probably to store the extension in the database, that way you're not scanning your file system for matches.

However, you could also use the glob();; function and check if results has anything.

$result = glob('images/img_' . $post->_id . '.*');

If you want to further narrow down your extension types you can do that like this:

$result = glob('images/img_' . $post->_id . '.{jpg,jpeg,png,gif}', GLOB_BRACE)

New code might look something like this (untested but you get the idea):

$result = glob('images/img_' . $post->_id . '.{jpg,jpeg,png,gif}', GLOB_BRACE);

if(!empty($result)) { 
    //show the image
    echo '<img src="' . $result[0]. '" class="img-responsive" />';
} else {
    // show default image 
    echo '<img src="images/default.gif" class="img-responsive" />'; 
}

Upvotes: 1

Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

You could loop through an array of file extensions and check them i've made a sample script to show what i mean listed below

$array = array('.jpg','.png','.gif');
foreach($array as $key => $value){

    $file = 'images/img_' . $post->_id . $value; // images/img_1.jpg
    if (file_exists($file) && is_file($file)) {
       $filefound = '<img src="' . $file . '" class="img-responsive" />';
    } 
} 

if(isset($filefound)){
    echo $filefound; 
}else{
    echo '<img src="images/default.gif" class="img-responsive" />'; 
}

Upvotes: 1

Harshal
Harshal

Reputation: 3622

It is obvious that your script will check only for .jpg not for others because you have written statically the file extension.

$file = 'images/img_' . $post->_id . '.jpg'; 
                                         ^

Now Make change in this line to make available the extension dynamically. I am supposing that you are storing the image name with extension than:

   $file = 'images/img_' . $post->_id . $post->image_name; 

Note : $post->image_name should be like :

$post->image_name = abc.png (image_name.extension);

Upvotes: 0

Related Questions