Luke Snowden
Luke Snowden

Reputation: 4196

php - get file type from contents

I'm on the lookout for a function which gets the file type from the contents of a file.

ie.

public function wideImage( $width, $height, $cropped, $file )
{
    $cacheForInMinues = 15;
    $cacheName = md5( $file . $width . $height . $cropped );
    if ( Cache::has( "images.{$cacheName}" ) )
    {
        $image = Cache::get( "images.{$cacheName}" );
        // get_file_type_from_contents( $image );
    }
    else
    {
        ....

regards

Upvotes: 1

Views: 128

Answers (2)

David Ansermot
David Ansermot

Reputation: 6112

You should read the header of the content. Take a look at this : http://www.mikekunz.com/image_file_header.html

But I don't know if there is a already made function to do this.

Upvotes: 0

Hüseyin BABAL
Hüseyin BABAL

Reputation: 15550

You can use fileinfo;

<?php
function get_file_type_from_contents($image) {
    $finfo = new finfo;
    $fileInfo = $finfo->file($image, FILEINFO_MIME);
    return $fileInfo;
}

Upvotes: 3

Related Questions