PHP- Get file type by URL

后端 未结 7 1320
太阳男子
太阳男子 2020-12-31 16:54

I want to get the file type (eg. image/gif) by URL using PHP.I had tried



        
7条回答
  •  再見小時候
    2020-12-31 17:50

    You are not going wrong anywhere. exif_imagetype returns the value of one of the image type constants: http://php.net/manual/en/image.constants.php

    If you would like to convert this to an extension string, you could use a switch statement:

    $typeString = null;
    $typeInt = exif_imagetype($image_path);
    switch($typeInt) {
      case IMG_GIF:
        $typeString = 'image/gif';
        break;
      case IMG_JPG:
        $typeString = 'image/jpg';
        break;
      case IMG_JPEG:
        $typeString = 'image/jpeg';
        break;
      case IMG_PNG:
        $typeString = 'image/png';
        break;
      case IMG_WBMP:
        $typeString = 'image/wbmp';
        break;
      case IMG_XPM:
        $typeString = 'image/xpm';
        break;
      default: 
        $typeString = 'unknown';
    }
    

    You may want to change the order to most to least frequently expected for better performance.

提交回复
热议问题