Alternative finfo () for php <5.3

 <?php $finfo = new finfo(); $fileinfo = $finfo->file($_FILES["fileToUpload"]["tmp_name"], FILEINFO_MIME); switch($fileinfo) { case "image/gif": case "image/jpeg": case "image/png": move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "upload/" . $_FILES["fileToUpload"]["name"]); echo "Your file has successfully been uploaded, and is awaiting moderator approval for points." . "<html><br><a href='uploadfile.php'>Upload more.</a>"; break; default: echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb"; break; } ?> 

I was recently brought to my attention, there is nothing wrong with it, it just doesn’t work, because my server php only in 5.2 lemma know if you guys can find a way to make it work using MIME

+6
source share
4 answers
+5
source

On Linux servers you can be lazy and use:

  $type = exec("file -iL " . escapeshellcmd($fn) . " 2>/dev/null"); $type = trim(strtok(substr(strrchr($type, ":"), 1), ";")); 
+1
source

mime_content_type may still work for you. Although it is now located in the fileinfo section of the manual, it existed before fileinfo was added to the PHP core.

Please note that a small configuration may be required if your host moved the Apache mime.types file from its normal location, as described in the comments on this page.

+1
source

Note. I know that this does not directly answer the question about the version of PHP. However, I found this post trying to solve my problem, and therefore it may be useful to someone in the future.

I also recently tried to deal with the Fileinfo library, trying to check MP3 files. I realized that there are known issues with Fileinfo and MP3 files, even if you correctly configured the magic database file for your environment.

If Fileinfo cannot determine the type of MP3 mime, it can return application/octet-stream . Not very useful when checking a file.

As an alternative, I started using the following system command. This is very similar to the @marios suggestion and still seems more reliable than Fileinfo .

 $path = 'path/to/your/mp3/file.mp3'; $mime = exec('file -b --mime-type ' . $path); 

I tested this on Ubuntu 10.04 and OSX Mountain Lion, so I assume it works on most Unix environments. I believe that there are some Windows ports.

In truth, I'm not quite sure how safe or reliable this method is, but I saw that it recommended <a href = ""> a few times here in Stackoverflow. If anyone has more information, please share!

+1
source

All Articles