Download Remote File

Posted by Abs on Stack Overflow See other posts from Stack Overflow or by Abs
Published on 2010-04-25T18:25:11Z Indexed on 2010/04/25 18:33 UTC
Read the original article Hit count: 176

Filed under:
|

Hello all,

I have a function that will be passed in a link. The link is to a remote image. I thought I could just use the extension of the file in the URL to determine the type of image but some URLs won't have extensions in the URL. They probably just push headers to the browser and therefore I do not have an extension to parse from the URL.

How can I test if the URL has an extension and if not then read the headers to determine the file type?

Am I over complicating things here? Is there an easier way to do this? I am making use of Codeigniter maybe there is something already built in to do this?

All I really want to do is download an image from a URL with the correct extension.

This is what I have so far.

function get_image($image_link){

    $remoteFile = $image_link;

    $ext = ''; //some URLs might not have an extension

    $file = fopen($remoteFile, "r");


    if (!$file) {

        return false;

    }else{

        $line = '';

        while (!feof ($file)) {

            $line .= fgets ($file, 4096);

        }

        $file_name = time().$ext;

        file_put_contents($file_name,  $line);

    }

    fclose($file);

}

Thanks all for any help

© Stack Overflow or respective owner

Related posts about php

Related posts about codeigniter