Deviant Logo

How to check if there’s an image in the folder using php?

post details top
Jun 9th, 2010
post details top


I want to code an if else statement to check if there’s an image in the folder.
EX. i want to check if apple.jpg is in the image folder or not

if apple.jpg exists, echo
else echo

what statement do i use to check if there’s the name of the image file in a certain folder?

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

5 Comments

  • NoLimit

    You can use the function file_exists

    < ?php
    $filename = '/path/file.jpg';

    if (file_exists($filename)) {
    print "Le fichier $filename exist";
    } else {
    print "Le fichier $filename doesn't exist";
    }
    ?>

  • Dhanraj

    < ?php
    $filename = '/path/file.jpg';

    if (file_exists($filename)) {
    print "Le fichier $filename exist";
    } else {
    print "Le fichier $filename doesn't exist";
    }
    ?>

  • lkas12

    You can also add “else if” parts to the If Statements you’ve been exploring in the previous sections. The syntax is this:

    else if (another_condition_to_test) {

    }

    Change your code to this, to see how else if works:

    < ?PHP

    $kitten_image = 1;
    $church_image = 0;

    if ($kitten_image == 1) {
    print ("“);
    }
    else if ($church_image == 1){
    print (““);
    }
    else {
    print (“No value of 1 detected”);
    }

    ?>

    Here’s we’re just testing to see which of our variables holds a value of 1. But notice the “else if” lines (and that there’s a space between else and if):

    else if ($church_image == 1){
    print (““);
    }

    What you’re saying is “If the previous if statement isn’t true, then try this one.” PHP will then try to evaluate the new condition. If it’s true (the $church_image variable holds a value of 1), then the code between the new curly brackets gets executes. If it’s false (the $church_image variable does NOT holds a value of 1), then the line of code will be ignored, and PHP will move on.

    To catch any other eventualities, we have an “else” part at the end. Notice that all parts (if, else if, and else) are neatly sectioned of with pairs of curly brackets:

    if ($kitten_image == 1) {

    }

    else if ($church_image == 1) {

    }

    else {

    }

Leave a Reply

Search

  • Partner links