Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,486 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,206 people online right now. Registration is fast and FREE... Join Now!




Recognizing .pdf and .doc on uploaded files for icons

 
Reply to this topicStart new topic

Recognizing .pdf and .doc on uploaded files for icons

King8654
post 29 Jun, 2008 - 12:48 PM
Post #1


D.I.C Head

Group Icon
Joined: 4 Mar, 2008
Posts: 91


My Contributions


Hey

Currently on site, people upload exams to the site, either in doc or pdf. Each has their own icon to show in their description table, which is generated using:

CODE

<?
                  $f=explode(".",$row[3]);
                if($f[1]=="doc")
                {
                    echo "<img src=images/wordlogo.jpg><br>";
                }
                else
                if($f[1]=="pdf")
                {
                       echo "<img src=images/pdflogo.jpg><br>";
                
                                }
          ?>


This works great, UNLESS the uploaded file name has .'s in it like math.final.exam. Math final exam works fine with this code, but the one with .'s in it throws it off, since it looks for the last . and then reads whatever is after it. Is there any way to make this work regardless of whether the user uploads a file with multiple periods in it or not??

Thanks alot

User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 29 Jun, 2008 - 03:01 PM
Post #2


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


what you can do is use substr to read the last 3 letters or better than that grab the extension alltogether like this

CODE

<?php

$str = 'math.final.exam.doc';

$ext = substr($str,strrpos($str,'.')+1);

echo $ext;
?>

this would echo "doc"
User is offlineProfile CardPM

Go to the top of the page

King8654
post 29 Jun, 2008 - 04:19 PM
Post #3


D.I.C Head

Group Icon
Joined: 4 Mar, 2008
Posts: 91


My Contributions


Thanks for the response

but the $str will be different for each exam on the site, determined by the link the user clicks on. How can i grab the file name and place it in $str.
User is offlineProfile CardPM

Go to the top of the page

MRJ
post 29 Jun, 2008 - 04:39 PM
Post #4


D.I.C Head

Group Icon
Joined: 13 Oct, 2007
Posts: 83



Thanked 1 times
My Contributions


I believe that joeyadms Code is just an example of looking from the right hand side instead of the left so that it should work for any string.

User is offlineProfile CardPM

Go to the top of the page

King8654
post 29 Jun, 2008 - 04:45 PM
Post #5


D.I.C Head

Group Icon
Joined: 4 Mar, 2008
Posts: 91


My Contributions


Yes, i understand that it will work with any string, but in the example its math.final.exam, and im asking how to have the string be whatever the exam title is the user clicks on instead of math.final.exam
User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 30 Jun, 2008 - 06:17 AM
Post #6


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


Assuming $row[3] holds the filename, it would look something like this.

CODE

<?php

$fileName = $row[3];
$fileExt = substr($fileName,strrpos($fileName,'.')+1);
if($fileExt == "doc"){
    echo "<img src=images/wordlogo.jpg><br>";
} elseif($fileExt == "pdf"){
    echo "<img src=images/pdflogo.jpg><br>";
}

?>
User is offlineProfile CardPM

Go to the top of the page

kapax
post 2 Jul, 2008 - 05:02 AM
Post #7


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 39



Thanked 1 times
My Contributions


I see your problem.

I think you may have problems even if you distinguish different extensions. What if user tries to upload a different type of file?

I am not sure about .doc files, but in every .pdf file there is a "%PDF" in the beginning of the source. So, you can easily check whether file is .pdf or not just by opening and checking the first 4 characters.
User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 2 Jul, 2008 - 06:53 AM
Post #8


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


^ All he wants to do is if an uploaded file is pdf show the pdf icon, if it is doc show the doc icon.

The problem he was having was he was looking for the '.' in the file and reading the last letters as the extension, but you can see how a problem would arise if someone uploaded a file called 'something.something.doc' , the script would think 'something.doc' was the extension.

You do not need to descend into the file to solve it, all you need to do is search for the '.' going reverse, starting from the end of the string and moving to the front, that way, no matter what the filename is, you'll always have the extension.
wink2.gif
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 2 Jul, 2008 - 07:04 AM
Post #9


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,255



Thanked 14 times

Dream Kudos: 650
My Contributions


Couldn't you do it even simpler by using a regular expression and the $ anchor? Something like this should work, I would think: \.([^.]+)$
User is online!Profile CardPM

Go to the top of the page

joeyadms
post 2 Jul, 2008 - 01:21 PM
Post #10


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


You pay for regex considerably with performance. The preg* functions are faster these days, but simple searches, like the one here, can be done using strrpos, and substr, they are low cost string manipulations and will work much better than regex.

Using regex you would be doing the same thing with less performance.
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 2 Jul, 2008 - 01:24 PM
Post #11


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,255



Thanked 14 times

Dream Kudos: 650
My Contributions


Ah - fair enough, then. I have to say that I don't use PHP much, this just seemed like a spot where regular expressions would fit.

User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 03:40PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month