Re: Getting MIME-type of file

From: Date: Thu, 08 Feb 2001 19:05:29 +0000
Subject: Re: Getting MIME-type of file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-38801@lists.php.net to get a copy of this message
Yes: Assuming that then input variable for the file is "file": like: <input type="file" name="file"> The returned results would be: $file = the name of the temporary file on the server. $file_name = The actual name of the file uploaded (ie pic.jpg) $file_size = the size of the file, in bytes. $file_type = the MIME type (yay) of the file. So, if you uploaded a Jpeg to your server, you could echo each of those variables to get info on them. You can also specify what files you want / don't want... Something like this might do it: $allowed_types = array("image/gif","image/pjpeg","image/jpeg"); if (!in_array($file_type/* Our type of file */,$allowed_types) { echo "You uploaded $file_name. Unfortunately, we don't allow files with the MIME type, $file_type."; } else { echo "You uploaded $file_name, which has a MIME type of $file_type. Your file was $file_size. Thanks :)"; } Of course, you will have to do more extensive checking, and write some code to unlink invalid file types, together with auto-rename if you expect, for example, a lot of people to be uploading membership details: If they include a pic, out of two people uploading a picture, say "tobias.jpg", one of them is going to be disappointed; One file will overwrite the other if put into the same directory :) Also, you'll probably want to set a maximum files size. $max_file_size = "102400"; /* in bytes (100Kb in this example) */ if ($file_size > $max_file_size) { echo "Your file's too big!"; } Hope this helps. James. > After a lot of searching on the mailing list and different resources I have > come up with... nothing. > > My question: > Can I get the MIME-type of a file using PHP? > (image/gif, text/plain, application/octet-stream, ect.) > > Thanks, > // Tobias > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#38801) next »