HTML 上传表单无法将 RAW 格式识别为图像,而是使用 'application/octet-stream'。我如何验证?

HTML upload form not recognising RAW format as images, using 'application/octet-stream' instead. How do I validate?

我正在创建一个允许用户上传 RAW 格式图像的上传屏幕。有多种 RAW 格式,我有一个预定义的列表。

define ('ALLOWED_FILE_TYPE', array(
    "image/jpeg", 
    "image/jpg", 
    "image/tif", 
    "image/png", 
    "image/bmp", 
    "image/x-canon-cr2",
    "image/x-canon-crw", 
    "image/x-kodak-dcr",  
    "image/x-nikon-nef", 
    "image/x-olympus-orf", 
    "image/x-sony-arw",
    "image/x-adobe-dng",
    "image/x-epson-erf",
    "image/x-kodak-k25",
    "image/x-kodak-kdc",
    "image/x-minolta-mrw",
    "image/x-pentax-pef",
    "image/x-fuji-raf",
    "image/x-panasonic-raw",
    "image/x-sony-sr2",
    "image/x-sony-srf",
    "image/x-sigma-x3f"
    ));

(我在 Whosebug 上找到了这个列表。我还没有机会检查它的真实性。)

我的脚本是一个标准的上传表单,它调用了一个方法来检查上传文件类型。不幸的是,对于 RAW 文件,返回的文件类型是 'application/octet-stream':

array (
  'name' => 'DSCF0450.RAF',
  'type' => 'application/octet-stream',
  'tmp_name' => '/private/var/tmp/phpVFT8BJ',
  'error' => 0,
  'size' => 50560000,
)

很明显,RAW 文件没有被识别为图像。

谁能告诉我为什么?

我是否必须将上传表单的加密类型更改为 enctype="multipart/form-data" 以外的类型?

谁能告诉我如何将原始文件识别为图像?

N.B:

处理图像不是问题。我正在尝试验证上传。

我已经尝试使用 finfo 和 finfo_file 来获取文件类型(它也是 returns 'application/octet-stream')。

我知道我可以获得文件扩展名并进行检查。我宁愿不依赖可以轻易改变的东西,除非我绝对必须这样做。

提前致谢。

Can anyone tell me why?

上传文件的content-type由客户端提供。它被报告为 application/octet-stream(即无法识别格式的一些数据字节),因为 browser/OS 没有上传它不识别文件类型。

Do I have to change the upload form enctype to something other than enctype="multipart/form-data"?

没有。为文件报告的 content-type 是为上传的 部分 指定的(由 多个 部分 组成表单数据).

Can anyone tell me how to get the raw file recognised as an image?

除非对访问您网站的每个浏览器进行更改(除非在非常不寻常的情况下,否则这是不切实际的),您不能。至少现在不是。

I am trying to validate the upload.

通过信任 content-type header 由客户端 isn't safe anyway 发送 来验证文件是否为图像。

I have tried using finfo and finfo_file to get a file type (it also returns 'application/octet-stream').

可能与客户无法识别的原因相同。 RAW 文件明显 non-standard.

Processing the image is not an issue.

那么您用来处理它的工具可能是确定它是否是您想要的数据类型的最佳方式。