简体   繁体   English

使用 php 检查文件是否为存档(zip 或 rar)

[英]Check if a file is archive (zip or rar) using php

How can i check if a file is archived (zip or rar) without knowing the extension.如何在不知道扩展名的情况下检查文件是否已存档(zip 或 rar)。 I need to find it using php.我需要使用 php 找到它。

I cannot use Fileinfo because its not installed and installation of any other packages on the server its out of the question.我无法使用 Fileinfo,因为它没有安装并且在服务器上安装任何其他软件包都是不可能的。

UPDATE:更新:

Zip module its not installed and i cannot install extra packages. Zip 模块未安装,我无法安装额外的软件包。 I don't want to use mime_content_type because its deprecated我不想使用 mime_content_type 因为它已弃用

Thank you谢谢

Output from od -c : od -c输出:

  0000000    R   a   r   ! 032  \a  \0 317 220   s  \0  \0  \r  \0  \0  \0

  0000000    P   K 003 004  \n  \0  \0  \0  \0  \0  \0  \0   !  \0  \0  \0

You could use something like this:你可以使用这样的东西:

<?php

$fh = @fopen($argv[1], "r");

if (!$fh) {
  print "ERROR: couldn't open file.\n";
  exit(126);
}

$blob = fgets($fh, 5);

fclose($fh);

if (strpos($blob, 'Rar') !== false) {
  print "Looks like a Rar.\n";
} else
if (strpos($blob, 'PK') !== false) {
  print "Looks like a ZIP.\n";
} else {
  print "I dunno.\n";
  exit(1);
}

?>

And my output:我的输出:

ghoti@baz:~ 423$ ./filephp.php A2.rar
Looks like a Rar.
ghoti@baz:~ 424$ ./filephp.php OLDIE.zip 
Looks like a ZIP.
ghoti@baz:~ 425$ ./filephp.php 1-11-1.PDF 
I dunno.
ghoti@baz:~ 426$ 

To test whether a file is a zip archive, you can attempt to open it as a zip using open_zip function.要测试文件是否为 zip 存档,您可以尝试使用open_zip函数将其作为 zip 文件打开。 For rar, you need to have PECL rar (preferably version at least 2.0.0) installed - see http://php.net/manual/en/book.rar.php for more details.对于 rar,您需要安装 PECL rar(最好版本至少为 2.0.0) - 有关更多详细信息,请参阅http://php.net/manual/en/book.rar.php The code could look like this:代码可能如下所示:

if(is_resource($zip = zip_open($filename)))
{
    zip_close($zip);
    //this is a zip archive
}
elseif(($rar = RarArchive::open($filename)) !== FALSE)
{
    $rar->close();
    //this is a rar archive
}
else
{
    //this is not a zip or rar archive
}

You may need to do a bit extra work if the archives are password-protected.如果档案受密码保护,您可能需要做一些额外的工作。 Read the corresponding php manual pages for more details.阅读相应的 php 手册页以获取更多详细信息。

Read first 10 bytes of the file.读取文件的前 10 个字节。 If they are (80, 75, 3, 4, 20, 0, 0, 0, 8, 0) it is a ZIP file.如果它们是 (80, 75, 3, 4, 20, 0, 0, 0, 8, 0) 它是一个 ZIP 文件。 RAR files have the following 7 first bytes: (82, 97, 114, 33, 26, 7, 0) If you open a ZIP file in a text editor (for instance, Notepad++) you will see: PK[ETX][EOT][DC4][NUL][NUL][NUL][BS][NUL]....-> the Ascii codes for the characters are listed above. RAR 文件有以下 7 个首字节:(82, 97, 114, 33, 26, 7, 0) 如果您在文本编辑器(例如 Notepad++)中打开 ZIP 文件,您将看到: PK[ETX][EOT ][DC4][NUL][NUL][NUL][BS][NUL]....-> 上面列出了字符的 Ascii 代码。 For the RAR files the picture is: Rar![SUB][BEL][NUL].... So, just read the 10 first bytes of a file and you can tell if it is ZIP or RAR archive.对于 RAR 文件,图片是:Rar![SUB][BEL][NUL].... 所以,只要读取文件的前 10 个字节,您就可以判断它是 ZIP 还是 RAR 存档。 Cheers干杯

The fileinfo functions should help you with this, by checking the file's mime type: fileinfo函数应该可以通过检查文件的 MIME 类型来帮助您:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename); // This will return the mime-type
finfo_close($finfo);

You could output the info from unix file command and parse it(assuming you can execute system commands, which is bad practice).您可以从 unix file 命令输出信息并解析它(假设您可以执行系统命令,这是不好的做法)。

This is example of centos "file filename " output.这是centos“文件文件”输出的示例。

[rr@localhost images] (master)# file ui-anim_basic_16x16.gif ui-anim_basic_16x16.gif: GIF image data, version 89a, 16 x 16 [rr@localhost images] (master)# file ui-anim_basic_16x16.gif ui-anim_basic_16x16.gif:GIF 图像数据,版本 89a,16 x 16

[rr@localhost images] (master)# file ui-icons_454545_256x240.png ui-icons_454545_256x240.png: PNG image data, 256 x 240, 8-bit colormap, non-interlaced [rr@localhost images] (master)# file ui-icons_454545_256x240.png ui-icons_454545_256x240.png:PNG 图像数据,256 x 240,8 位色图,非隔行

[rr@localhost vendors] (master)# file jquery-validation-1.9.0.zip jquery-validation-1.9.0.zip: Zip archive data, at least v1.0 to extract [rr@localhost vendor] (master)# file jquery-validation-1.9.0.zip jquery-validation-1.9.0.zip:压缩存档数据,至少要解压v1.0

also like other people suggested, you could read few bytes and check if they match signature.也像其他人建议的那样,您可以读取几个字节并检查它们是否匹配签名。

for rar为 rar

Identifying characters Hex: 52 61 72 21 1A 07 00 , ASCII: Rar!识别字符 Hex: 52 61 72 21 1A 07 00 , ASCII: Rar!

for zip用于拉链

Identifying characters Hex: 50 4B 03 04 , ASCII: PK识别字符 Hex: 50 4B 03 04 , ASCII: PK

<?php

function isZipFile($filepath){
    $fh = fopen($filepath,'r');
    $bytes = fread($fh,4);
    fclose($fh);
    return ('504b0304' === bin2hex($bytes));
}

Thanks to flu for the helpful link to the zip file specification.感谢flu提供指向zip文件规范的有用链接。

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

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