Page 1 of 2
using explode to determin filetypes
Posted: Thu Jan 04, 2007 3:32 am
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
Posted: Thu Jan 04, 2007 3:46 am
by onion2k
Have you tried the ZIP functions?
http://uk.php.net/zip ... zip_open() might be a good one to look at.
Posted: Thu Jan 04, 2007 4:13 am
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
Posted: Thu Jan 04, 2007 4:30 am
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.
Posted: Thu Jan 04, 2007 6:23 am
by mcog_esteban
Posted: Thu Jan 04, 2007 10:56 am
by Kieran Huggins
Always use mime_content_type() instead of trusting the filename!
Posted: Thu Jan 04, 2007 11:14 am
by feyd
mime_content_type() uses the filename.
Posted: Thu Jan 04, 2007 11:32 am
by volka
As parameter

But not to determine the filetype.
Posted: Thu Jan 04, 2007 11:47 am
by feyd
It uses magic.mime which is based on the filename. Not the composition of the file.
Posted: Thu Jan 04, 2007 11:50 am
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)
Posted: Thu Jan 04, 2007 12:08 pm
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

Posted: Thu Jan 04, 2007 12:08 pm
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
Posted: Thu Jan 04, 2007 12:11 pm
by feyd
I don't remember it working. Oh well, I won't use it anyways. Cross platform issues.

Posted: Thu Jan 04, 2007 12:12 pm
by volka
What cross-platform issues? What do you use instead?
Posted: Thu Jan 04, 2007 12:14 pm
by feyd
Uh, mime.magic isn't on windows servers.