SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast Liquide's Avatar
    Join Date
    Oct 2002
    Location
    Katrineholm, Sweden
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    * pondering * remote files

    Just a quick question really, is it possible to dtermine a remote (a file on anther server) file's mime-type.

    The reason I ask is that I'm making a thumbnail script that creates thumbnails by reading remote files and creating a dump php file to generate a smaller thumbnail (cache setting set to 10 days to reduce bandwith).

    Therefore I have to ensure that the files actually are images (no checking the file extensions is not enough for me :P )

  2. #2
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why is checking the file extension not good enough for you?

    Yes, someone could put any type of data and change the file extension to .gif, but by the same token they could put any type of data in a file, change the extension to .gif, and send back a valid gif image mime type.

    If it's security your looking for you wont gain any there. And if your looking to simply avoid someone accidently using a URL to a non-image file, extension checking should be sufficient.

  3. #3
    SitePoint Enthusiast Liquide's Avatar
    Join Date
    Oct 2002
    Location
    Katrineholm, Sweden
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyngon
    Why is checking the file extension not good enough for you?

    Yes, someone could put any type of data and change the file extension to .gif, but by the same token they could put any type of data in a file, change the extension to .gif, and send back a valid gif image mime type.

    If it's security your looking for you wont gain any there. And if your looking to simply avoid someone accidently using a URL to a non-image file, extension checking should be sufficient.
    Well it would work, but and here comes the but some of the files that I know of that I'll have to use via the thumbnail script will in essence be a php-file that simulates an image

    That is why I have to check that mime-type, ah well will see what I can do with the exentension alone then.

  4. #4
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see! Sorry about the misunderstanding.

    Hmm... well I think in order to get the mime type you'd have to send an HTTP request for the content and read the mime type line from it. No idea how to do it though.

  5. #5
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do this:
    PHP Code:
     $headers apache_request_headers();
     echo 
    $headers['Type']; 
    you can use $headers['Type'] anyway you find necessary.

    Aaron
    Aaron Brazell
    Technosailor



  6. #6
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, but that would just get the info for the page... hmmm, how would you send a request to an image and find the type.... hmmm.

    You could do this:
    PHP Code:
     $headers apache_request_headers();
     if(empty(
    $headers['Type']))
       {
       
    # requested file is a PHP, HTML or other web PAGE
       
    }
     else
       {
       
    # Requested item is an image
       

    That still might not work but maybe it'll get you going in the right direction...
    Aaron Brazell
    Technosailor



  7. #7
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function get_mime($_url)
    {
        
    $a_url parse_url($_url);
        if (!
    is_array($a_url)) return false;
        
        
    $fp fsockopen($a_url['host'], 80$errno$errstr5);
        if (!
    $fp) return false;
        
        
    $query = (isset($a_url['query'])) ? '?'.$a_url['query'] : '';
        
    $request "HEAD ".$a_url['path'].$query." HTTP/1.1\r\nHost: ".$a_url['host']."\r\n\r\n";
        
        
    fputs($fp$request);
        
    $answ '';
        while(!
    feof($fp))
            
    $answ .= fgets($fp1024);
        
    fclose($fp);
        
        if (
    preg_match('/Content-Type:(.*)/'$answ$match)) return $match[1];
        else return 
    false;
    }

    // Example use
    echo get_mime('http://de2.php.net/images/php_snow.gif'); // prints image/gif 

  8. #8
    SitePoint Enthusiast Liquide's Avatar
    Join Date
    Oct 2002
    Location
    Katrineholm, Sweden
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gaheris
    PHP Code:
    function get_mime($_url)
    {
        
    $a_url parse_url($_url);
        if (!
    is_array($a_url)) return false;
        
        
    $fp fsockopen($a_url['host'], 80$errno$errstr5);
        if (!
    $fp) return false;
        
        
    $query = (isset($a_url['query'])) ? '?'.$a_url['query'] : '';
        
    $request "HEAD ".$a_url['path'].$query." HTTP/1.1\r\nHost: ".$a_url['host']."\r\n\r\n";
        
        
    fputs($fp$request);
        
    $answ '';
        while(!
    feof($fp))
            
    $answ .= fgets($fp1024);
        
    fclose($fp);
        
        if (
    preg_match('/Content-Type:(.*)/'$answ$match)) return $match[1];
        else return 
    false;
    }

    // Example use
    echo get_mime('http://de2.php.net/images/php_snow.gif'); // prints image/gif 
    thanks, worked like a charm

  9. #9
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    5secs to time out is way too much :P
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •