Jason Axelrod
Jason Axelrod

Reputation: 7805

Multiple File Exists Checking? A Better way?

I have a script. It recieves a variable called $node, which is a string; for now, lets assume the variable value is "NODEVALUE". When the script is called, it takes the variable $node, and tries to find an image called NODEVALUE.png. If it cant find that image, it then checks for NODEVALUE.jpg, if it can't find that it looks for NODEVALUE.gif... and after all that, it still cant find, it returns RANDOM.png.

Right now I am doing this script as follows:

if (file_exists($img = $node.".png")) {  }
else if (file_exists($img = $node.".jpg")) {  }
else if (file_exists($img = $node.".gif")) {  }
else
{
    $img = 'RANDOM.png';
}

There has to be a better way than this... anyone have any ideas?

Upvotes: 5

Views: 6689

Answers (4)

DoubleZero
DoubleZero

Reputation: 31

$n_folder="images/nodes/";
$u_folder="images/users/";
     $extensions=array(".png",".jpg",".gif");

foreach ($extensions as $ext)
{
    if (file_exists($n_folder.$node.$ext))
    {
     $img=$n_folder.$node.$ext;
     break;
    }
    elseif (file_exists($u_folder.$node.$ext))
    {
      $img=$u_folder.$node.$ext;
     break;
    }
}

if (!$img)
{
    random image generator script...
}

Upvotes: 2

Jason Axelrod
Jason Axelrod

Reputation: 7805

Okay... this is what I finalized on:

$searches = array(
    $folder . "nodes/" . $node . ".png",
    $folder . "nodes/" . $node . ".jpg",
    $folder . "nodes/" . $node . ".gif",
    $folder . "users/" . $user . ".png",
    $folder . "users/" . $user . ".jpg",
    $folder . "users/" . $user . ".gif"
);

foreach ($searches AS $search)
{
    if (file_exists($search))
    {
        $img = $search;
        break;
    }
}

if (!$img)
{
    random image generator script...
}

Upvotes: 1

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99921

$list = array_filter(array("$node.png", "$node.jpg", "$node.gif"), 'file_exists');
if (!$img = array_shift($list)) {
    $img = 'RANDOM.png';
}

Alternatives :

$list = scandir(".");
$list = preg_grep("#".preg_quote($node,'#')."\.(jpg|png|gif)$#", $list);

This returns a list of file names that start with $node and with a .jpg, .png or .gif suffix.

If the directory contains many entries, if may be faster to use glob() first:

$list = glob("$node.*"); // take care to escape $node here
$list = preg_grep("#".preg_quote($node,'#')."\.(jpg|png|gif)$#");

The preg_grep() can also be replaced by

$list = array_intersect($list, array("$node.png", "$node.jpg", "$node.gif"));

Or with a loop:

$img = null;
foreach(array('png','jpg','gif') as $ext) {
    if (!file_exists("$node.$ext")) continue;
    $img = "$node.$ext"; break;
}
$img = $img ? $img : "RANDOM.png";

Upvotes: 3

mario
mario

Reputation: 145482

The most compact (and therefore not recommended) form would be:

if (array_sum(array_map("file_exists", array($fn1, $fn2, $fn3)))) {

It could be adapted to also returning the found filename using array_search:

array_search(1, array_map("file_exists", array($fn1=>$fn1, $fn2=>$fn2)))

Hardly readable. Note how it also requires a map like array("$node.png"=>"$node.png", "$node.gif"=>"$node.gif", ...). So it would not be that much shorter.

Upvotes: 2

Related Questions