Mime Type

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

Post Reply
leinad256
Forum Newbie
Posts: 4
Joined: Sat Apr 19, 2003 2:44 am

Mime Type

Post by leinad256 »

I tried, I looked and I almost died...but I couldnt find it.

I need to know if a file is jpg, but looking the extension isnt enough and I cant use the mime_content_type function because I have php 4.2 and this function is avaible until php 4.3.

Its not a email attachment or upload file, then the $_FILE (or the array where it contains the uploaded file...I forgot the name) doesnt work me.

Some ideas???

I will be glad you if you can help

thx everybody
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if your script is running on a unix-system you might use file with php's exec functions
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

getimagesize -- second indices return

example
$a = getimagesize($_FILES['fieldname']['tmp_name']);
if($a[2] !== 2) { // not a valid jpeg file }

$a[2] would return 1 = gif, 2 = jpeg, 3 = png, 4 = swf.......
leinad256
Forum Newbie
Posts: 4
Joined: Sat Apr 19, 2003 2:44 am

Post by leinad256 »

Thanks by your answers, I have a Mac OS X and it is a UNIX based System, maybe it will work. The getimagesize function doesnt work me because if the file isnt a image the php send me a error.


Thxxxxx


Best Regards from Mexico
leinad256
Forum Newbie
Posts: 4
Joined: Sat Apr 19, 2003 2:44 am

Post by leinad256 »

Sorry, I dont kwon which exec function I need. I tried to look the mime type with the file function but it return me a lot of unreable code. Can you give me more details???


Thx man
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I think for separating jpegs from non-jepgs pootergeist's suggestion is better.
What exactly is the error you get?
to avoid a warning that might be printed (undefined index 2 in $a) try

Code: Select all

<?php
$fileToCheck = 'notAJpeg.dat';
$a = @getimagesize($fileToCheck);
if (count($a) < 1 || $a[2] != 2)
	echo 'not a jpeg';
else
	echo 'all right';
?>
leinad256
Forum Newbie
Posts: 4
Joined: Sat Apr 19, 2003 2:44 am

Post by leinad256 »

thats right!!!!!!!!

I didnt realize about the @ operator



thx very much!!!!!!!!!!!!!!!!!!!!!!

when u come to mexico i gonna invite u some tequilas :D



thx volka, thx pootergeist
Post Reply