Inserting images based on file extension

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ty44ler
Forum Newbie
Posts: 3
Joined: Wed Jun 03, 2009 12:01 pm

Inserting images based on file extension

Post 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>");
      }
   }
}
?>
 
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Inserting images based on file extension

Post 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?
Ty44ler
Forum Newbie
Posts: 3
Joined: Wed Jun 03, 2009 12:01 pm

Re: Inserting images based on file extension

Post by Ty44ler »

Ty44ler
Forum Newbie
Posts: 3
Joined: Wed Jun 03, 2009 12:01 pm

Re: Inserting images based on file extension

Post by Ty44ler »

I just checked the output code... all the images are coming up as unknown...

Why is that?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Inserting images based on file extension

Post by mikemike »

Working here :)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Inserting images based on file extension

Post 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'];
Post Reply