当用户发布图像URL时,我正在尝试保存图像。我正在用这段代码来做这件事:
$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
如何保存每个图像文件,即使 url 包含"奇怪"字符?
更新:我已经回显了我的 $_POST['form_pic'],当我在提交后发布 url 时,似乎"+"字符被空格替换了?
使用这个,
$pic = str_replace("+","_",$pic);