src/Tar.php
public function filetype($file)
    {
        // for existing files, try to read the magic bytes
        if(file_exists($file) && is_readable($file) && filesize($file) > 5) {
            $fh = @fopen($file, 'rb');
            if(!$fh) return false;
            $magic = fread($fh, 5);
            fclose($fh);

            if(strpos($magic, "\x42\x5a") === 0) return Archive::COMPRESS_BZIP;
            if(strpos($magic, "\x1f\x8b") === 0) return Archive::COMPRESS_GZIP;
        }

        // otherwise rely on file name
        $file = strtolower($file);
        if (substr($file, -3) == '.gz' || substr($file, -4) == '.tgz') {
            return Archive::COMPRESS_GZIP;
        } elseif (substr($file, -4) == '.bz2' || substr($file, -4) == '.tbz') {
            return Archive::COMPRESS_BZIP;
        }

        return Archive::COMPRESS_NONE;
    }
public function filetype($file)
    {
        // for existing files, try to read the magic bytes
        if(file_exists($file) && is_readable($file) && filesize($file) > 5) {
            $fh = @fopen($file, 'rb');
            if(!$fh) return false;
            $magic = fread($fh, 5);
            fclose($fh);

            if(strpos($magic, "\x42\x5a") === 0) return Archive::COMPRESS_BZIP;
            if(strpos($magic, "\x1f\x8b") === 0) return Archive::COMPRESS_GZIP;
        }

        // otherwise rely on file name
        $file = strtolower($file);
        if (substr($file, -3) == '.gz' || substr($file, -4) == '.tgz') {
            return Archive::COMPRESS_GZIP;
        } elseif (substr($file, -4) == '.bz2' || substr($file, -4) == '.tbz') {
            return Archive::COMPRESS_BZIP;
        }

        return Archive::COMPRESS_NONE;
    }
public function filetype ( $ file ) { // for existing files, try to read the magic bytes if ( file_exists ( $ file ) && is_readable ( $ file ) && filesize ( $ file ) > 5 ) { $ fh = @ fopen ( $ file , 'rb' ) ; if ( ! $ fh ) return false ; $ magic = fread ( $ fh , 5 ) ; fclose ( $ fh ) ; if ( strpos ( $ magic , "\x42\x5a" ) === 0 ) return Archive :: COMPRESS_BZIP ; if ( strpos ( $ magic , "\x1f\x8b" ) === 0 ) return Archive :: COMPRESS_GZIP ; } // otherwise rely on file name $ file = strtolower ( $ file ) ; if ( substr ( $ file , - 3 ) == '.gz' || substr ( $ file , - 4 ) == '.tgz' ) { return Archive :: COMPRESS_GZIP ; } elseif ( substr ( $ file , - 4 ) == '.bz2' || substr ( $ file , - 4 ) == '.tbz' ) { return Archive :: COMPRESS_BZIP ; } return Archive :: COMPRESS_NONE ; }
Guesses the wanted compression from the given file

Uses magic bytes for existing files, the file extension otherwise

You don't need to call this yourself. It's used when you pass Archive::COMPRESS_AUTO somewhere

@param string $file
@return int
Guesses the wanted compression from the given file