如果图片网址包含“+”字符则无法保存图片

时间:2012-12-01 09:34:20

标签: php file-get-contents scrape

我正在尝试在用户发布图片网址时保存图片。 我正在使用此代码执行此操作:

        $pic = $Db->escape($_POST['form_pic']); // (escape is a function to mysql_real_escape_string)
        $time = strtotime("now");
        $filename = $item_id.'_'.$time.'.'.$ext;
        $item_pic = 'img/ads/'.$filename;

        $contents = file_get_contents($pic);
        file_put_contents('../img/ads/'. $filename, $contents);

这适用于大多数情况。但是当url包含一个像“+”这样的奇怪字符时,上面的代码就不起作用了。他没有保存图像。

 An example url: http://2.bp.blogspot.com/-ubBMWObG6u0/T-m3zYIq3CI/AAAAAAAABxU/Z8aa1Dgny9c/s1600/Grown+sunglasses.jpg

即使网址包含“奇怪”字符,如何保存每个图像文件?

更新:我已经回复了我的$ _POST ['form_pic'],当我在提交后发布网址时,似乎是“+”字符被空格替换了?

1 个答案:

答案 0 :(得分:-1)

使用此,

$pic = str_replace("+","_",$pic);