PHP how can i check if a file is mp3 or image file

2020-01-23 12:27发布

how can i check if a file is an mp3 file or image file, other than check each possible extension?

11条回答
▲ chillily
2楼-- · 2020-01-23 12:30

getimageinfo is best to find images . Check if return type is false .

查看更多
三岁会撩人
3楼-- · 2020-01-23 12:32

You could use finfo like this:

$mime = finfo_open(FILEINFO_MIME, $path_to_mime_magic_file);
if ($mime ===FALSE) {
    throw new Exception ('Finfo could not be run');
}
$filetype = finfo_file($mime, $filename);
finfo_close($mime);

or if you have problems with finfo not being installed, or the mime magic file just not working (it works correctly on 3 out of our 4 servers - all identical OS and PHP installs) - then try using Linux's native file (don't forget to sanitise the filename though: in this example, I know the filename can be trusted as it's a PHP temporary filename in my test code):

ob_start();
system('file -i -b '.$filename);
$output = ob_get_clean();
$output = explode("; ", $output);
if (is_array($output)) {
     $filetype = trim($output[0]);
}

Then just pass the mime file type to a switch statement like:

switch (strtolower($filetype)) {
            case 'image/gif':
                return '.gif';
                break;
            case 'image/png':
                return '.png';
                break;
            case 'image/jpeg':
                return '.jpg';
                break;
            case 'audio/mpeg':
                return '.mp3';
                break;
}
return null;
查看更多
太酷不给撩
4楼-- · 2020-01-23 12:35

You can identify image files using getimagesize.

To find out more about MP3 and other audio/video files, I have been recommended php-mp4info getID3().

查看更多
该账号已被封号
5楼-- · 2020-01-23 12:37

This function checks if the file is an image based on extension and mime and returns true if it's a browser compatible image...

function checkImage($image) {

//checks if the file is a browser compatible image

$mimes = array('image/gif','image/jpeg','image/pjpeg','image/png');
//get mime type
$mime = getimagesize($image);
$mime = $mime['mime'];

$extensions = array('jpg','png','gif','jpeg');
$extension = strtolower( pathinfo( $image, PATHINFO_EXTENSION ) );

if ( in_array( $extension , $extensions ) AND in_array( $mime, $mimes ) ) return TRUE; 
else return FALSE; 

}
查看更多
放荡不羁爱自由
6楼-- · 2020-01-23 12:39

The best way is to use finfo_file function. Example:

<?php 
if (isset($_FILES['yourfilename']['tmp_name'])) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['yourfilename']['tmp_name']);
    if ($mime == 'image/jpg') {
        echo "It's an jpg image!";
    }
    finfo_close($finfo);
}
?>
查看更多
Evening l夕情丶
7楼-- · 2020-01-23 12:44

This function get a file path and with use finfo_open and mime_content_type if supported, return image or video or audio string.

/**
 * get file type
 * @return image, video, audio
 */
public static function getFileType($file)
{
    if (function_exists('finfo_open')) {
        if ($info = finfo_open(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) {
            $mimeType = finfo_file($info, $file);
        }
    } elseif (function_exists('mime_content_type')) {
        $mimeType = mime_content_type($file);
    }
    if (strstr($mimeType, 'image/')) {
        return 'image';
    } else if (strstr($mimeType, 'video/')) {
        return 'video';
    } else if (strstr($mimeType, 'audio/')) {
        return 'audio';
    } else {
        return null;
    }
}
查看更多
登录 后发表回答