简体   繁体   English

如何检查文件类型

[英]How to check file type

I'll check on server side (PHP) if a file is a real . 我将在服务器端(PHP)上检查文件是否为real。 txt or not. txt At the moment I've wrote : 此刻我写了:

if(substr($_FILES['tracklistFile']['name'], -3)=='txt')

but I think that its not the best solution! 但是我认为这不是最好的解决方案! How can I check it? 我该如何检查?

To reliably get the file extension, use this: 为了可靠地获取文件扩展名,请使用以下命令:

pathinfo($_FILES['tracklistFile']['name'], PATHINFO_EXTENSION);

That will, however, only give you just that - the extension part of the filename. 但是,那只会给您-文件名的扩展部分。 It says nothing about the actual type of the file. 它没有说明文件的实际类型。

If the file is a local file on your server (as opposed to being uploaded), you could call the file command available on most Unix-based operating systems. 如果文件是服务器上的本地文件(而不是上传),则可以调用大多数基于Unix的操作系统上可用的file命令。

Uhm...for example if a user call picture.jpg as picture.txt this check will fail, and my script can create many errors :) Won't be under control :) 嗯...例如,如果用户将picture.jpg作为picture.txt调用,则此检查将失败,并且我的脚本可能会创建许多错误:)不会受到控制:)

Okay, but by what criteria would you tell the invalid JPG apart from a valid text file? 好的,但是根据什么标准,除了有效的文本文件之外,您还会告诉无效的JPG吗? A text file could theoretically contain anything . 理论上,文本文件可以包含任何内容 It has no defined format whatsoever, it just consists of a number of completely arbitrary bytes. 它没有任何定义的格式,仅由许多完全任意的字节组成。

MIME type detection as suggested by @Sardine will have the same problem; @Sardine建议的MIME类型检测将有相同的问题。 however, it will be able to detect many file types that are not a text file, so you can reject those. 但是,它将能够检测到许多文本文件的文件类型,因此您可以拒绝这些文件类型。 Still, it will not be 100% reliable. 但是,它不是100%可靠的。

You will need to think of some rules that allow you to recognize a text file, depending on what your script is going do with them. 您将需要考虑一些规则,这些规则使您可以识别文本文件,具体取决于脚本对它们的处理方式。

If it's just for the sake of finding out whether it's a text file, I would consider not doing any checks at all. 如果只是为了确定它是否是文本文件,我将考虑完全不进行任何检查。 You just need to make sure it gets treated as a text file in every step of your application (ie if it contains PHP, JavaScript or HTML code, it won't be executed). 您只需要确保在应用程序的每个步骤中将其视为文本文件即可(即,如果其中包含PHP,JavaScript或HTML代码,则将不会执行该文件)。

If possible, you can check for the mime type of the file which should be 如果可能,您可以检查文件的MIME类型,该类型应为

text/plain

This is unfortunately a tricky task in PHP. 不幸的是,这是PHP中的一项棘手任务。 If you are using PHP4, you can use the following: 如果您使用的是PHP4,则可以使用以下代码:

$mtype = mime_content_type($file_path);

If you have PHP >= 5.3.0 & PECL fileinfo >= 0.1.0, you can use something like: 如果您的PHP> = 5.3.0和PECL fileinfo> = 0.1.0,则可以使用以下代码:

$finfo = finfo_open(FILEINFO_MIME);
$mtype = finfo_file($finfo, $file_path);
finfo_close($finfo);

If neither of these work, I would recommend at least making sure it isn't something like a picture using methods discussed in other answers. 如果以上两种方法均无效,则建议至少使用其他答案中讨论的方法来确保它不像图片。 Without being able to use these functions, you can only do the unfortunate method of checking the file name to determine the file type, which is not at all reliable. 无法使用这些功能,您只能执行不幸的检查文件名的方法来确定文件类型,这根本不可靠。

Sources: 资料来源:

http://www.php.net/manual/en/book.fileinfo.php http://www.php.net/manual/zh/book.fileinfo.php

http://forums.digitalpoint.com/showthread.php?t=522166 http://forums.digitalpoint.com/showthread.php?t=522166

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM