473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Uploads and mime type

Hi folks

I'm a bit confused with an upload script that I've written. I want to be
able to check the file type of an upload by checking the mime type but I'm
not getting the results that I thought I should. According to what I've
read .zip files should return something like 'application/zip' or
'application/x-zip-compressed', RAR should return
'application/x-rar-compressed' but both return 'application/octet-stream'.
I'm getting the results that I expected when checking .jpg, .png of .gif
files.

I'm checking the mime type using $_FILES['userfile']['type']

Anyone throw some light on this for me?
Nov 15 '07 #1
6 6228
On Thu, 15 Nov 2007 22:59:03 +0100, Mad Hatter <co***@class31.co.ukwrote:
Hi folks

I'm a bit confused with an upload script that I've written. I want to be
able to check the file type of an upload by checking the mime type but
I'm
not getting the results that I thought I should. According to what I've
read .zip files should return something like 'application/zip' or
'application/x-zip-compressed', RAR should return
'application/x-rar-compressed' but both return
'application/octet-stream'.
I'm getting the results that I expected when checking .jpg, .png of .gif
files.

I'm checking the mime type using $_FILES['userfile']['type']
The mime-type is user-submitted, totally unreliable and shouldn't be
trusted for any further processing. application/octet-stream is usually
the fallback for a binary file for which the the UA either doesn't know or
doesn't care about the mime-type.

If the mime-type is really important to you, try the fileinfo function
(http://nl2.php.net/manual/en/ref.fileinfo.php).

What's the reason the mime-type is of importance to you?
--
Rik Wasmus
Nov 15 '07 #2
On Fri, 16 Nov 2007 00:17:42 +0100, Rik Wasmus wrote:
Hi
>
What's the reason the mime-type is of importance to you?
It's not. It's just that all the upload scripts that I've seen seem to use
it to work out the file type. I think I'll stick to my original idea of
checking the file extension of the uploaded file.

Thanks for the reply :)
Nov 15 '07 #3
..oO(Mad Hatter)
>On Fri, 16 Nov 2007 00:17:42 +0100, Rik Wasmus wrote:
>>
What's the reason the mime-type is of importance to you?

It's not. It's just that all the upload scripts that I've seen seem to use
it to work out the file type. I think I'll stick to my original idea of
checking the file extension of the uploaded file.
The extension is as unreliable as the submitted content type. Don't use
it for security purposes.

Micha
Nov 16 '07 #4
The extension is as unreliable as the submitted content type. Don't use
it for security purposes.
What's the safest way of checking the file type? The script isn't visible
to site users but I would rather be safe just in case someone finds it.
Nov 16 '07 #5
On 16 Nov, 08:36, Mad Hatter <co...@class31.co.ukwrote:
The extension is as unreliable as the submitted content type. Don't use
it for security purposes.

What's the safest way of checking the file type? The script isn't visible
to site users but I would rather be safe just in case someone finds it.
To check a file's mime type

http://uk.php.net/manual/en/ref.fileinfo.php

or http://uk.php.net/manual/en/function...ntent-type.php if you
have PECL installed.

Personally, I just stick to using file extensions. Most supplied MIME
types are application/octet-stream anyway.

A quick and dirty way to get the extension:

$extension = strrev(substr(strrev($_FILES['userfile']['name']), 0,
strpos(strrev($_FILES['userfile']['name']), '.')));

(It reverses a string so that it gets the first from the end then
reverses the result)

Nov 16 '07 #6
Hi
>
A quick and dirty way to get the extension:

$extension = strrev(substr(strrev($_FILES['userfile']['name']), 0,
strpos(strrev($_FILES['userfile']['name']), '.')));

(It reverses a string so that it gets the first from the end then
reverses the result)
That's a much better method than the one I was going to use :-
$upname=($_FILES["userfile"]["name"]);
$extension = substr($upname, strrpos($upname, '.') + 1);

Thanks for that :)
Nov 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phil Powell | last post by:
How do you detect an image MIME type if you know of the image in a directory? For example, I know of: if (is_file("$myImagePath/$myImageName")) { // FIND MIME TYPE BUT HOW I DUNNO } The...
0
by: prefetch | last post by:
hi people - i'm new to python, but i'm quickly becoming a fan. i'm using BaseHTTPServer and wrote a do_POST method, which i'd like to read posted files and write them to disk. seems like it...
6
by: nwheavyw8 | last post by:
I am currently trying to write a simple PHP script that will split an uploading file up into 500kb "chunks", then read and concatenate them back together when accessed for download. I can't seem...
0
by: clevariant | last post by:
Hello, I'm tasked with creating a WSDL file that is compatible with .NET's code gen' tool, wsdl.exe. I'm getting an error in the code output saying "Missing soap:body input binding", which...
1
by: Michael Loughry | last post by:
I'm writing a web application that fetches documents from the server and sends them to the client. I do this using the Response.BinaryWrite method. However, is there any way to detect the MIME...
12
by: JMB | last post by:
Hello, I was wondering if anyone knew of any projects extending the inline upload progress bar to utilize an inpage image uploader with bar, without having to refresh or go to a seperate page,...
5
by: splodge | last post by:
This may seem like a stupid question but I want to check before I go ahead and build this... I am working on a portal, part of which allows users to upload files. Part of the array within...
0
by: José Joye | last post by:
hello, I have to communicate with a web Service for which I received the WSDL below. When trying to generate the client proxy, it gives me the error (see below). After investigation, I have...
6
by: squishywaffle | last post by:
This has got me somewhat stumped, so I'll throw it up here in hopes that someone has ran into this before. I'm trying to send a MIME package to Esko Backstage. I'm a bit confused as to how to send...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.