Vamsi Krishna B
Vamsi Krishna B

Reputation: 11490

download remote image in codeigniter

I want to download a remotely hosted image using codeiogniter . and a validation feature also has to be added so that only images are downloaded.. is it possible to use the built in functions offered by CI ??

thanks .

Upvotes: 1

Views: 2492

Answers (2)

Carlos Mora
Carlos Mora

Reputation: 1184

This solution is not from CI but PHP, think it can be usefull anyway. May be checking the image size is enough to be sure it's an image, like

$imagedata = getimagesize('http://othersite.com/image.jpg');

or use exif_imagetype

if ( ! function_exists( 'exif_imagetype' ) ) {
    function exif_imagetype ( $filename ) {
        if ( ( list($width, $height, $type, $attr) = getimagesize( $filename ) ) !== false ) {
            return $type;
        }
    return false;
    }
}


if( exif_imagetype('http://othersite.com/image.jpg') ) 
{  
//is an image 
}

Upvotes: 2

jfoucher
jfoucher

Reputation: 2281

To get a remote image, you can just use file_get_contents, assuming allow_url_fopen is set to on in php.ini.

$image=file_get_contents('http://othersite.com/image.jpg');

Once you have that, you can validate the file extension, or try and resize the image to check that it really is an image.

Upvotes: 4

Related Questions