Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative to finfo() for php < 5.3

Tags:

php

<?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;
        }


?>

it has recently been brought to my attention there is nothing wrong here, it just doesnt work because my servers php is only at 5.2 lemme know if you guys can find a way to make it work using MIME

like image 290
Shawn Avatar asked Mar 22 '11 22:03

Shawn


3 Answers

pecl install fileinfo?

http://pecl.php.net/package/Fileinfo

like image 138
Mikel Avatar answered Sep 22 '22 06:09

Mikel


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), ";"));
like image 38
mario Avatar answered Sep 22 '22 06:09

mario


mime_content_type might still work for you. While it's now under the fileinfo section in the manual, it existed way before fileinfo was brought into the PHP core.

Do note that it might require a bit of configuration if your host moved Apache's mime.types file out of the normal location, as documented in the comments on that page.

like image 1
Charles Avatar answered Sep 22 '22 06:09

Charles