PHP - copy()在带有花括号/括号的URL上失败

时间:2017-12-01 14:56:42

标签: php url str-replace

我在PHP中遇到了copy()函数的问题。

我需要将一个类似于@Path("/") @Api(value="/swagger/apidocs") public interface ApiClient { @GET @Path("endpoint/consult") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation(value = "Consult test Endpoint", response = Response.class) public Response consultEndpoint() throws ServiceException; } 的远程网址复制到我的本地驱动器。

以下是我的代码失败的部分,以及 $ RemoteURL 变量的一些上下文:

https://example.co.uk/{8d988e90-a325-4a1c-a340-a489166286b8}/{14409287-2c29-4b51-91e4-0891b5619659}/main/imgnew-(2).jpg

然而,这会引发错误:

$replace = array('%7B', '%7D','%28','%29');
$entities = array('{', '}','(',')');
$RemoteURL = str_replace($entities, $replace, "https://example.co.uk/{8d988e90-a325-4a1c-a340-a489166286b8}/{14409287-2c29-4b51-91e4-0891b5619659}/main/imgnew-(2).jpg");

$PicName = "new.jpg"

if(copy($RemoteURL,"C:\Users\Me\Downloads\Pictures\" . $PicName)){
    echo "<script>console.log(\"(" . $RemoteURL . ") copied to waiting.\")</script>";
} else {
    echo "<p class='float red'>READ ERROR</p>";
}

我到底错过了什么,或者PHP对URL本身不喜欢什么?

2 个答案:

答案 0 :(得分:1)

原来产生上述错误的问题是我有一个有问题的重音字符。

è发出错误,这是通过创建替换某些部分的函数来修复的:

function URLEncodeRules($string) {
    $replacements = array('%C3%A9','%C3%A8');
    $entities = array('é','è');
    return str_replace($entities, $replacements, $string);
}

$RemoteURL = URLEncodeRules($CaptureRow['URL']);

if(copy($RemoteURL,"image.jpg")){
   echo "Success!;
}

答案 1 :(得分:0)

尝试使用url_encode();

希望这会对你有所帮助。

谢谢,

相关问题