File Type Icon Script/Function

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
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

File Type Icon Script/Function

Post by drewrockshard »

Guys,

I need some help one again. It's been a little while since I've called upon for help, but here it is...

I'm basically building a very, VERY simple file uploader for my site, so that at the spur of the moment, I can quickly upload a file or two to my webserver. I was going to add a few minor features, and one of them being a File Type Icon Script/Function, however, I'm not able to get it to work. Here's a few snippets of the related code:

The following is the array that basically says "for this extension, use this filename (for the image)":

Code: Select all

 
$filetypes = array (
                'png' => 'jpg.gif',
                'jpeg' => 'jpg.gif',
                'bmp' => 'jpg.gif',
                'jpg' => 'jpg.gif',
                'gif' => 'gif.gif',
                'zip' => 'zip.gif',
                'rar' => 'rar.gif',
                'exe' => 'exe.gif',
                'setup' => 'setup.gif',
                'txt' => 'text.png',
                'htm' => 'html.gif',
                'html' => 'html.gif',
                'fla' => 'fla.gif',
                'swf' => 'swf.gif',
                'xls' => 'xls.gif',
                'doc' => 'doc.gif',
                'sig' => 'sig.gif',
                'fh10' => 'fh10.gif',
                'pdf' => 'pdf.gif',
                'psd' => 'psd.gif',
                'rm' => 'real.gif',
                'mpg' => 'video.gif',
                'mpeg' => 'video.gif',
                'mov' => 'video2.gif',
                'avi' => 'video.gif',
                'eps' => 'eps.gif',
                'gz' => 'archive.png',
                'asc' => 'sig.gif',
                'wma' => 'audio.jpg',
                'mp3' => 'audio.jpg',
        'unknown' => 'error.gif',
            );  
 
The following is the function I am trying to code so that the extension can be called from the table (HTML):

Code: Select all

 
// getIcon
    function getIcon($ext) {
        $icon = $ext;
        if($filetypes[$icon]) {
            $icon = $filetypes[$icon];
        } else {
            $icon = 'error.gif';
        }
        return $icon;
    }
 
The following is the HTML markup section (remember all these are snippets from my code, so don't worry about the variables):

Code: Select all

<tr>
                                <td><img src="_resources/<?php echo getIcon($ext); ?>" /></td>
Basically, the
$ext
outputs "rar" or "exe" or "zip" and so on, depending on the extension of each filename - this works. The part that doesn't is the function. Whenever I execute the code, it automatically just gets "error.gif" and displays it. I might be calling it wrong or my code just might be really messed up. I've even done some thorough testing to make sure that the PHP function itself is at least getting the extension. So I believe the part I am getting wrong is the following:

Code: Select all

if($filetypes[$icon]) {
Not sure though. If anyone can shed some light and help out, I would be much appreciated.

Thanks again,
Drew
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: File Type Icon Script/Function

Post by andyhoneycutt »

Your problem is a matter of scope. You've declared "$filetypes[]" outside the viewable scope of your function. You'll want it inside your function, or use define() or some other method to make your types globally accessible.

-Andy
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Re: File Type Icon Script/Function

Post by drewrockshard »

andyhoneycutt wrote:Your problem is a matter of scope. You've declared "$filetypes[]" outside the viewable scope of your function. You'll want it inside your function, or use define() or some other method to make your types globally accessible.

-Andy
Andy,

Thank you :) I used the global keyword to fix it :)

Your help was much appreciated.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: File Type Icon Script/Function

Post by andyhoneycutt »

Very good then, glad I could help!
Post Reply