5

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?

Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78

4 Answers4

3
$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";
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • The thing about this is that I'm worried about performance... array_filter would run the file_exist function on every outcome. If the first outcome works, there is no reason to run the other outcomes... its just a waste of resources. – Jason Axelrod Jan 22 '11 at 20:27
  • Okay... the loop you did at the end is perfect. Except I simply changed it to `if (file_exists(stuff)) { $img = stuff; break; }` everything else is just redunant. – Jason Axelrod Jan 22 '11 at 20:50
2

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.

mario
  • 144,265
  • 20
  • 237
  • 291
  • The thing about this is that I'm worried about performance... array_map would run the file_exist function on every outcome. If the first outcome works, there is no reason to run the other outcomes... its just a waste of resources. – Jason Axelrod Jan 22 '11 at 20:26
  • @JasonAxelrod, It's indeed a waste of processing. Not sure if it's a measurable performance impact though. You would have to test it with xdebug/cachegrind. If it's a frequently checked condition, then you should definitely stay with your original code. – mario Jan 22 '11 at 20:40
2
$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...
}
DoubleZero
  • 31
  • 1
  • 5
  • seams like i didnt understood good... but thats because you didnt explain good! You have file_exists function in your question wich means you are checking if file exists... So you trying to check if string contains image.jpg, gif,png or whatever and if it dont than return random.png? – DoubleZero Jan 22 '11 at 20:33
1

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...
}
Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78