Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4896

How to download image from a url provided in php

I have an xml feed that has some image urls provided in it . Each link contains one Images only,

I have to parse this url and download those images in some folder using PHP.

Something like this

while($xmlcontent = mysql_fetch_array($images)){

                         download_img($xmlcontent["tag"]);
                }
function download_img($im_url)
{


}

Thanks in Advance

Upvotes: 1

Views: 2984

Answers (2)

SO-user
SO-user

Reputation: 1456

Put this line in an htaccess file in the directory you want the setting to be enabled:

php_value allow_url_fopen On

then create a function as simple as this.

function download_file($file_url, $save_to)
{
    $content = file_get_contents($file_url);
    file_put_contents($save_to, $content);
}

usage:

download_file('http://example.com/images/logo.jpg', realpath("./downloads") . '/yourfile.jpg');

Upvotes: 2

KIKO Software
KIKO Software

Reputation: 16759

I assume the URL can be anywhere? Try this:

$image = file_get_contents($url);
file_put_contents($filename,$image);

See: http://php.net/manual/en/wrappers.php

And yes, copy() should also work if you believe that page.

Upvotes: 1

Related Questions