我试图在用户发布图像网址时保存图像。 我正在使用以下代码执行此操作:
$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'] ,似乎当我在提交后发布网址时,“+”字符被空格替换了?
用这个,
$pic = str_replace("+","_",$pic);