Page 1 of 1

Inserting images based on file extension

Posted: Wed Jun 03, 2009 12:14 pm
by Ty44ler
What I'm trying to do:
I am calling a list of files from a folder and I'm trying to display a little icon of the file type next to it in the list. Also I'm calling sub-folder names and trying to display an image of a folder next to that.

What is going wrong:
I can get one file type to show up which is the first adobe pdf icon. no others images next to the file types show up and when the file name has a . in the middle of it, the image fails to show as well. Also, folders are listed but not being detected and the correct image shown...

Any ideas? I'm really new to this, so if you could explain things like you would to a small child that would be great. Thanks!

Code: Select all

<?php
$ignore = array(".","..","FTPCss.css","images","Thumbs.db");
 
$fileImage = array(
   'pdf' => 'images/Adobe_PDF_icon.png',
   'jpg' => 'images/jpeg_icon.jpeg');
 
while(false != ($file = readdir($dir))){
   if(!in_array($file,$ignore)){
      if(is_dir($file))
      {
         echo ("<p><a href=\"$file\"> <img src='images/folder.jpeg' /> $file</a> </p>");
      }else{
         $FileParts = explode(',',$file);
         $FileExtension = strtolower($FileParts[count($FileParts) - 1]);
         echo("<p><a href=\"$file\">".(isset($fileImage[$FileExtension]) ? "<img src='".$fileImage[$FileExtension]."' />" : "<img src='unknown.gif' />")." $file</a> </p>");
      }
   }
}
?>
 

Re: Inserting images based on file extension

Posted: Wed Jun 03, 2009 12:39 pm
by mikemike
Can you provide a link to where you're trying this so we can see if there are any errors and what's actually being produced by the script?

Re: Inserting images based on file extension

Posted: Wed Jun 03, 2009 12:55 pm
by Ty44ler

Re: Inserting images based on file extension

Posted: Wed Jun 03, 2009 2:26 pm
by Ty44ler
I just checked the output code... all the images are coming up as unknown...

Why is that?

Re: Inserting images based on file extension

Posted: Wed Jun 03, 2009 5:49 pm
by mikemike
Working here :)

Re: Inserting images based on file extension

Posted: Wed Jun 03, 2009 8:19 pm
by mikemike
even better...

Code: Select all

$file = '/link/to/path.jpg';
$pathinfo = pathinfo($file);
echo '<pre>'.print_r($pathinfo, true).'</pre>';
Run that for a load of useful array entries, including:
- Directory
- Basename
- Filename
- And, most importantly for us, extension

Code: Select all

echo $pathinfo['extension'];