Search code examples
phpfileinfo

fileinfo function PHP


Can someone give me an example of how to use fileinfo, to replace a snippet of code such as:

($_FILES["fileToUpload"]["type"] == "image/gif"
|| $_FILES["fileToUpload"]["type"] == "image/jpeg"
|| $_FILES["fileToUpload"]["type"] == "image/png")

Solution

  • Using this:

    $finfo = new finfo();
    $fileinfo = $finfo->file($file, FILEINFO_MIME);
    

    $fileinfo should contain the correct MIME type which you would be able to use in a snippet like that, or in a switch statement like:

    switch($fileinfo) {
        case "image/gif":
        case "image/jpeg":
        case "image/png":
            // Code
            break;
    }