Jump to content

Recommended Posts

Posted

On my website I have added this form allowing authenticated users with special permissions to upload files. However, I want to limit the types of files they can upload, and checking the file's extension isn't the most reliable method. The use of mimetypes would be a better idea, so I started with that.

 

It appeared the function 'mime_content_type' didn't work:

Fatal error: Call to undefined function mime_content_type() in /home1/waflix/public_html/test/test.php on line 2

So I hoped I could use the PECL extension recommended here, but I had no such luck:

Fatal error: Class 'finfo' not found in /home1/waflix/public_html/test/test.php on line 2

 

Even after having searched the Internet for a while, I have not been able of finding a good solution. Is there any chance this extension could be installed on the server, or do I have to wait for PHP 6 of which I've heard the extension would be installed by default?

Posted

Have you thought about using cURL?

 

 

Nevermind. I posted that before I thought it through. Your uploading and not transloading.

 

Posted

Hi Walfix,

 

There is an easy fix for this.

 

First, you will want to use this function:

<?php
function mime_content_type($filename, $mimePath = './') {
  $fileext = substr(strrchr($filename, '.'), 1);
  if (empty($fileext)) return (false);
  $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileext\s)/i";
  $lines = file("$mimePath/mime.types");
  foreach($lines as $line) {
  if (substr($line, 0, 1) == '#') continue; // skip comments
  $line = rtrim($line) . " ";
  if (!preg_match($regex, $line, $matches)) continue; // no match to the extension
  return ($matches[1]);
  }
  return (false); // no match at all
}
?>

 

Then, download this file to your home folder:

http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

 

I recommend addind the following to the bottom:

application/x-httpd-php php

 

This is because by default it does not have the php extension in it,

 

Then, use the function like so:

 

 

<?php
$type = mime_content_type($filename);
?>

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...