note 88035 modified in function.finfo-file by felipe

From: Date: Sat, 17 Jan 2009 01:21:40 +0000
Subject: note 88035 modified in function.finfo-file by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-149356@lists.php.net to get a copy of this message
I thought to use fileinfo to check if a file was gzip or bzip2. However, the mime type of a compressed file is "data" because compression is an encoding rather than a type. gzip files begin with binary 1f8b. bzip2 files begin with magic bytes 'B' 'Z' 'h'. e.g. <?php $s = file_get_contents("somefilepath"); if ( bin2hex(substr($s,0,2)) == '1f8b' ) {/* could be a gzip file */} if( substr($s,0,3) == 'BZh' ){/* could be a bzip2 file */} ?> I am not an encoding expert. My only testing was using a few of my own encoded files. --was-- I thought to use fileinfo to check if a file was gzip or bzip2. However, the mime type of a compressed file is "data" because compression is an encoding rather than a type. gzip files begin with binary 1f8b. bzip2 files begin with magic bytes 'B' 'Z' 'h'. e.g. $s = file_get_contents("somefilepath"); if ( bin2hex(substr($s,0,2)) == '1f8b' ) { could be a gzip file } if( substr($s,0,3) == 'BZh' ){ could be a bzip2 file } I am not an encoding expert. My only testing was using a few of my own encoded files. http://php.net/manual/en/function.finfo-file.php

« previous php.notes (#149356) next »