using explode to determin filetypes

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

ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

using explode to determin filetypes

Post by ryuuka »

hello again

i'm building a upload website and now i need to make sure that only zip files are being uploaded.
setting mime types in the forms doesn't work correctly and i can get the hang of the php
get_mime type thing so i'm going to use explode for this.

for this i have a few questions:

1. I'm going to use explode at every . how do i reverse the ouput so that the filetype comes first?
2. How do i extract the extension from the string or array?
3. Any other ways of determening the file types?

thanks in advance

ps. sorry for the horrid spelling
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Have you tried the ZIP functions? http://uk.php.net/zip ... zip_open() might be a good one to look at.
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

have looked at it and it's not what i'm looking for.

allow me to be a little bit clearer:

i'm making a file upload website for distribution of large files to various clients of my company.
akin to megaupload and all those others. The employees are only allowed to upload zip files i want to check in the
part of the site that the file is being move to the server if it's a file type that's being allowed.

and the file that the clients need is a zip file that needs to be downloaded. they are not allowed to open
the files with the use of our website.

example of what i want with the explode function:


file name: ryuuka.the.man.upload.zip

Code: Select all

explode(".", ryuuka.the.man.upload.zip);


this should output: ryuuka the man upload zip
what i want is this : zip upload man the zip

now i want to take zip out and perform a loop like this:


if ($extension = zip)
{
//everything uploads etc
}
if ($extension != zip)
{
// not allowed to upload
}

hope this is of more use
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Well, array_reverse() will reverse the array so that the last element is first. Or you can just look at $array[count($array)-1] if you don't want to reverse it. Or you could use strtolower(strrchr($filename, ".")); to get everything after the last period. The problem with that though is that it relies on the extension being right. If I make a file called nasty_file_containing_a_virus.exe, rename it to nasty_file_containing_a_virus.zip, your code will happily accept it thinking that it's a zip file. If you examine the file itself (eg, see if zip_open() can open it) then you'll know if it's a zip file or not.
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post by mcog_esteban »

Is this what you are looking for?

http://pt.php.net/mime-content-type
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

mcog_esteban wrote:Is this what you are looking for?

http://pt.php.net/mime-content-type
Always use mime_content_type() instead of trusting the filename!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mime_content_type() uses the filename.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

feyd wrote:mime_content_type() uses the filename.
As parameter ;) But not to determine the filetype.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It uses magic.mime which is based on the filename. Not the composition of the file.
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

pathinfo() will get you the file extension, but that doesn't guarantee you that it's not just another filetype renamed with a .zip extension. If you really want to verify it's a zip, you could use the zip functions or at least check the first four bytes of the file to be: 50 4b 03 04 (hex)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

feyd wrote:It uses magic.mime which is based on the filename. Not the composition of the file.
Whaaaaaa????

All these years I've been led astray :-(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

feyd wrote:It uses magic.mime which is based on the filename. Not the composition of the file.
No it's not.
mime.magic wrote:0 leshort 0x602 application/x-alan-adventure-game
0 string TADS application/x-tads-game
0 short 0420 application/x-executable-file
0 short 0421 application/x-executable-file
0 leshort 0603 application/x-executable-file
0 string Core\001 application/x-executable-file
0 string AMANDA:\ TAPESTART\ DATE application/x-amanda-header
0 belong 0x000003f3 application/x-executable-file
0 belong 0x000003e7 application/x-library-file
0 belong 0x000001b3 video/mpeg
0 belong 0x000001ba video/mpeg
0 beshort&0xfff0 0xfff0 audio/mpeg
4 leshort 0xAF11 video/fli
4 leshort 0xAF12 video/flc
[...]
I don't see any filename information here.
Rename a zip archive to e.g. test.jpg and mime_content_type will still return application/zip based on
mime.magic wrote:0 string PK\003\004 application/zip
Last edited by volka on Thu Jan 04, 2007 12:12 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't remember it working. Oh well, I won't use it anyways. Cross platform issues. ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What cross-platform issues? What do you use instead?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Uh, mime.magic isn't on windows servers.
Post Reply