PDA

View Full Version : PHP Use either thumb.jpg OR thumb.gif, depending on which is present?



bernk
September 12th, 2008, 01:09 PM
Hi guys.

Here is the setup:

portfolio/project/thumb.jpg
portfolio/project/thumb.gif
portfolio/project/thumb.jpg
portfolio/project/thumb.jpg
...

You get the idea. If there is an image named thumb with either .jpg or .gif as the extension inside of each project folder I want my script to use it. Any suggestions?

imagined
September 12th, 2008, 02:59 PM
You using PHP?



$file = 'portfolio/project/thumb.jpg';

if(file_exists($file))
{

echo '<img src="'.$file.'" />';

}


something like this?

simplistik
September 12th, 2008, 05:01 PM
a function that i use, it only shows files w/ the specified extensions


<?php
function getFiles($folder)
{

$extensions = array();
$extensions['gif'] = 'image/gif';
$extensions['jpg'] = 'image/jpeg';
$extensions['jpeg'] = 'image/jpeg';
$extensions['png'] = 'image/png';

$files = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) )
{
$file_info = pathinfo($file);
if ( isset( $extensions[ strtolower( $file_info['extension'] ) ] ) )
{
$files[] = $file;
}
}
closedir($handle);

return $files;
}
?>