|
-
Mar 30th, 2008, 12:45 AM
#1
Thread Starter
WiggleWiggle
[RESOLVED] File Content Type
is there an easy way to get the content type of a file?
I am working on a file sharing script and i need to get the file content type for download.
the only way i can think of is like having a database with all the different content types and getting it by the file extension...
My usual boring signature: Something
-
Mar 30th, 2008, 09:12 AM
#2
Re: File Content Type
You could use the MIMEType extension to sniff for a content type. It is more reliable however to store the content type when the file is uploaded if possible.
-
Mar 30th, 2008, 11:19 AM
#3
Fanatic Member
Re: File Content Type
If you are on a *nix system, you can tell the script to execute:
And that will return the content type
:~$ file -bi hosts.allow
text/plain; charset=us-ascii
In fact this is what mediawiki does. Or can do I should say.
If you are on a windows box, there probably is a port of that program. And if you really wanted to get the content type, that would be the best way imo.
-
Mar 30th, 2008, 11:45 AM
#4
Thread Starter
WiggleWiggle
Re: File Content Type
 Originally Posted by penagate
You could use the MIMEType extension to sniff for a content type. It is more reliable however to store the content type when the file is uploaded if possible.
there are so many though. it would take forever to get them all.
My usual boring signature: Something
-
Mar 30th, 2008, 04:13 PM
#5
Re: File Content Type
 Originally Posted by dclamp
is there an easy way to get the content type of a file?
I am working on a file sharing script and i need to get the file content type for download.
the only way i can think of is like having a database with all the different content types and getting it by the file extension...
It is very difficult determine the exact file type of an external file; even when that file is just a text file. If the user uploading the file wants others to know the true file type then they will use the appropriate extension from which the browser can determine and send the correct MIME type for.
If the user wants to disguise the file type they can change the extension. To determine the true type you may need to open the file and check that it conforms to the appropriate format (e.g: XML,CSV). This is more difficult with binary files that have undocumented file formats.
Checking for executable files may involve looking for specific binary instruction combinations; but is by no means reliable and is even more difficult if the user has encoded or encrypted the file. In short; if you are doing this from a security perspective; the controls you can put in place a limited.
-
Mar 30th, 2008, 07:31 PM
#6
Re: File Content Type
 Originally Posted by dclamp
there are so many though. it would take forever to get them all.
Many what? A file can only have one content type. I'm not sure what you mean.
When a file is uploaded, the MIME type is sent by the client and available in the $_FILES array.
Maybe if you clarify what you are doing we can provide better assistance.
-
Mar 30th, 2008, 09:41 PM
#7
Thread Starter
WiggleWiggle
Re: File Content Type
oh. the $_FILES tells you what MIME type it is? i thought i would have to write code to figure out what MIME type it is
My usual boring signature: Something
-
Mar 30th, 2008, 09:44 PM
#8
-
Mar 30th, 2008, 10:39 PM
#9
Fanatic Member
Re: File Content Type
It is only available if its sent by the browser.
I don't know under what circumstances it doesn't, but I wouldn't touch it with a 6ft pole. I would use file -bi, that way you know for sure what the mime type is.
See http://www.php.net/manual/en/feature...load.php#53133
Also: http://www.php.net/manual/en/feature...load.php#52989
And: http://www.php.net/manual/en/feature...load.php#51256
Last edited by k1ll3rdr4g0n; Mar 30th, 2008 at 10:45 PM.
-
Mar 30th, 2008, 10:56 PM
#10
Re: File Content Type
You can't know for sure. All file with the -i flag does is use a 'magic' behaviour which is the same as the MIMEType and PECL/fileinfo extension. This is fallible; all type sniffing is. visualAd is correct — if this is for security purposes, there is not a lot you can realistically do.
Relying on the MIME type sent by the client is fine if you just need it to send back when streaming the file for download to another client. If the type is missing, you can simply use application/octet-stream to force a download. This is more than adequate for a file sharing service.
-
Mar 30th, 2008, 11:11 PM
#11
Thread Starter
WiggleWiggle
Re: File Content Type
i used application/octet-stream for a .doc file, and Word wasnt able to open it after i downloaded it. it said it was corrupt
My usual boring signature: Something
-
Mar 30th, 2008, 11:28 PM
#12
Re: File Content Type
That suggests you are not streaming it correctly...
-
Mar 30th, 2008, 11:35 PM
#13
Thread Starter
WiggleWiggle
Re: File Content Type
should i show you my code then?
My usual boring signature: Something
-
Mar 30th, 2008, 11:37 PM
#14
Re: File Content Type
At your leisure.
In fact, don't post it in code tags. Attach the .php file (if possible).
-
Mar 31st, 2008, 12:43 AM
#15
Thread Starter
WiggleWiggle
Re: File Content Type
thanks all. the $_FILES['file']['type'] solved it!
My usual boring signature: Something
-
Apr 9th, 2008, 08:22 AM
#16
Re: [RESOLVED] File Content Type
Dylan, the first four bytes of the file you upload can also be used to verify the file type. A JPEG file for example should always start with "ff d8 ff e0", if it doesn't then you can be absolutely sure that the file is not a JPEG. That doesn't prevent other data from being hidden within it but is is another step you can use:
PHP Code:
// example $jpeg = chr(0xff) . chr(0xd8) . chr(0xff) . chr(0xe0);
$fhwnd = fopen('file.jpg', 'rb');
$signature = fread($fhwnd, 4);
if ($signature != $jpeg) { // wrong file type }
You can see some other common signatures here: http://www.garykessler.net/library/file_sigs.html. Watch out though, some file types may have several signatures.
-
Apr 10th, 2008, 05:59 PM
#17
Thread Starter
WiggleWiggle
Re: [RESOLVED] File Content Type
thanks for that vis. i will look into it!
My usual boring signature: Something
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|