Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - PHP: mime_content_type return | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Gavin

Fuzzy
Rhinoceruses don't play games. They fucking charge your ass.
Level: 43

Posts: 523/799
EXP: 551711
For next: 13335

Since: 03-15-04
From: IL, USA

Since last post: 13 hours
Last activity: 13 hours
Posted on 03-05-05 10:55 PM Link | Quote
I have two classes, an upload and a gallery script, that require specific file type checking, so i decided to use mime_content_type. Since this function wasn't available by default, i did as instructed on the php.net documentation and enabled mime for use of the function (uncommenting ;extension=php_mime_magic.dll in php.ini, and because i'm developing on my windows machine using WAMP, adding mime_magic.magicfile = "H:\wamp\php\extras\magic.mime" to php.ini).

great fantastic, i thought, "well that really can't get any easier". Until, that is, i actually used the function in my script. For whatever reason it returns the same MIME type of text/plain no matter which file i throw at it.

i've taken my code out of context because it only obscures what i'm trying to explain, but even something as simple and explicity passed as the following outputs the wrong MIME format every time:

echo mime_content_type('screenshots\test\upe.png');

i mean it really can't get any more simple than that, at first i was tearing apart my class looking for where it was going wrong, but seeing the error occur in such a basic and (seemingly) correct statement as above, i'm wondering if there is some sort of server setting or another oversight on my part.

Has anyone else messed around with MIME php extensions, or perhaps had a similar error with setting up another extensions? This is rather frustrating as it seems that it should be working, and i haven't been able to find anything via google to help me out. Any comments would be appreciated
Jesper
Busy, busy, busy.
Level: 69

Posts: 2170/2390
EXP: 2856000
For next: 13743

Since: 03-15-04
From: Sweden.

Since last post: 176 days
Last activity: 79 days
Posted on 03-06-05 01:09 AM Link | Quote
If you're just dealing with images, which it seems you are, and you use PHP 4.3.0 or higher, why not use image_type_to_mime_type()? It has no extension dependencies (just the version dependency as mentioned above) but I don't know what it does when passed, say, a text file or something. Try and see, and just check that the mime type is not that (or better yet, confine it to jpegs, pngs and gifs to start with) to acheive valid data.

PHP extensions on Windows have the uncanny ability to fail at the drop of a hat, and I'm not going to swear by it, but the two times I've experienced the most problems were with Apache 2. The EXIF extension even gave me some flak because I didn't have a japanese text handling extension enabled, and when I turned that on it just crashed on me.
Gavin

Fuzzy
Rhinoceruses don't play games. They fucking charge your ass.
Level: 43

Posts: 524/799
EXP: 551711
For next: 13335

Since: 03-15-04
From: IL, USA

Since last post: 13 hours
Last activity: 13 hours
Posted on 03-06-05 03:49 AM Link | Quote
yah, EXIF seems to be giving me a shitload of trouble as well. i think i would have used exif_imagetype if it was working. instead, as it suggests on the page, it does the same thing as getimagesize but faster. I suppose i'll just stick to getimagesize for now then. my code turned out something like that (somewhat out of context):


//grab the files
$files_in_dir = glob($this->screenshot_dir.'{'.$this->accepted_exten.'}', GLOB_BRACE);

// aquire MIME file types of each alleged image
foreach ($files_in_dir as $key => $file)
{
//$file = getimagesize($file);
$MIMEtypes_in_dir[$key] = exif_imagetype($file);

// if true file types match up to accepted, display
if (in_array($MIMEtypes_in_dir[$key], $this->accepted_types))
{
echo ' < a href= " '.$files_in_dir[$key].' " > '.$files_in_dir[$key].'< /a >, ';
}
}


although somewhere in there i'm having to declare both the MIME file types and the file extensions, which is redundant and not very nice. i'll have to improve on that part later, as well as find a generic MIME checking device to use with my upload class that happens to not deal specifically with images (or, in the case that the code was correct, configure my server to run it)
Jesper
Busy, busy, busy.
Level: 69

Posts: 2176/2390
EXP: 2856000
For next: 13743

Since: 03-15-04
From: Sweden.

Since last post: 176 days
Last activity: 79 days
Posted on 03-06-05 04:54 AM Link | Quote
Is there a reason you're not using the function I recommended but instead a function dependent on an extension "giving you a shitload of trouble"? Do you just have PHP 4.2 on the 'live' server? That's the only blocker I can see here, basically.
Gavin

Fuzzy
Rhinoceruses don't play games. They fucking charge your ass.
Level: 43

Posts: 525/799
EXP: 551711
For next: 13335

Since: 03-15-04
From: IL, USA

Since last post: 13 hours
Last activity: 13 hours
Posted on 03-06-05 05:51 AM Link | Quote
because as i understand it exif_imagetype, as well as the getimagesize 'mime' index, uses something similar to mime_magic to search for cues and constants shared by an image type and is therefore evaluates the file type in spite of the file extension. i don't believe the function you gave me does that.

(key words being think and believe, i've never been very good at reading directions )
Jesper
Busy, busy, busy.
Level: 69

Posts: 2180/2390
EXP: 2856000
For next: 13743

Since: 03-15-04
From: Sweden.

Since last post: 176 days
Last activity: 79 days
Posted on 03-06-05 07:57 PM Link | Quote
Originally posted by Gavin
because as i understand it exif_imagetype, as well as the getimagesize 'mime' index, uses something similar to mime_magic to search for cues and constants shared by an image type and is therefore evaluates the file type in spite of the file extension. i don't believe the function you gave me does that.

(key words being think and believe, i've never been very good at reading directions )
Seeming as how MIME types are not something stored with a file but rather something sent *with* a file, all mime functions work like that, through recognizing headers and types of data and the like. So YES, the function I gave you does that.
Gavin

Fuzzy
Rhinoceruses don't play games. They fucking charge your ass.
Level: 43

Posts: 530/799
EXP: 551711
For next: 13335

Since: 03-15-04
From: IL, USA

Since last post: 13 hours
Last activity: 13 hours
Posted on 03-10-05 01:43 AM Link | Quote
forgive me if i'm just being retarded (happens all the time), but either i'm not reading it correctly or the function is actually a little but different than what you described.

string image_type_to_mime_type ( int imagetype)

it says it takes an integer for the imagetype, which i take to mean that it is for translating a numeric representation of a MIME type into a verbal expression, such as "text/plain" or whathaveyou. From what i'm reading, it is not what actually determines the MIME type, but just a translation tool

here's a portion of some code i lifted off some random site to illustrate:

function imcache($cachepath, $type, $function)
{
// Compose a uniqe'ish filename for cacheing.
$filename = urlencode($_SERVER["REQUEST_URI"]) . "---" . urlencode($function);
$filepath = realpath($cachepath)."/".$filename;


if(file_exists($filepath) and filectime($_SERVER["PATH_TRANSLATED"]) < filectime($filepath))
{
// if a file is there - stream response directly
header("Content-type: ".image_type_to_mime_type(exif_imagetype($filepath)));
@readfile($filepath);
touch($filepath);
}
else
{


(header("Content-type: ".image_type_to_mime_type(exif_imagetype($filepath))); being the highlighted and most important portion)

you'll see that in order for the image_type_to_mime_type to return a type, it is actually just given the numerical result of exif_imagetype($filepath) as it's index for retrieving the file textual representation of the file type. So the exif function actually does the work to get the file type.. or at least that's how i read it.

(edit: coincidentally i talked with another programmer who tried the mime function running on WAMP and he got the same set of errors i had. either we're both retarded, or just both out of luck for using windows)


(edited by Gavin on 03-10-05 11:05 AM)
Jesper
Busy, busy, busy.
Level: 69

Posts: 2228/2390
EXP: 2856000
For next: 13743

Since: 03-15-04
From: Sweden.

Since last post: 176 days
Last activity: 79 days
Posted on 03-11-05 10:54 PM Link | Quote
Using this:
<?php list($width, $height, $type, $uselesshtml) = getimagesize("omgimagename"); ?>

I'm *guessing* that these values should be compatible with the ones defined to be used with image_type_to_mime_type, but upon further inspection, the return value of getimagesize when caught as a variable (and not as a list of items as above) also includes the array key mime ($returnedvar['mime']) which should contain the text anyway. As this was introduced in the same version of PHP as image_type_to_mime_type, I revise my recommendation - you should simply use the following:

$foo = getimagesize("omgimagename"); header("Content-Type: ".$foo['mime']);
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - PHP: mime_content_type return | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.031 seconds.