如何从php中提供的URL下载图像

时间:2015-02-27 10:33:01

标签: php

我有一个xml Feed,其中提供了一些图片网址。每个链接仅包含一个图像,

我必须解析这个url并使用PHP将这些图像下载到某个文件夹中。

像这样的东西

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

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


}

先谢谢

2 个答案:

答案 0 :(得分:1)

将此行放在要启用该设置的目录中的htaccess文件中:

php_value allow_url_fopen On

然后创建一个简单的函数。

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

用法:

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

答案 1 :(得分:0)

我认为URL可以在任何地方吗?试试这个:

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

请参阅:http://php.net/manual/en/wrappers.php

是的,如果您相信该页面,copy()也应该有用。

相关问题