从PHP 5.5升级到5.6后,我的cURL上传失败了:
$aPost = array(
'file' => "@".$localFile,
'default_file' => 'html_version.html',
'expiration' => (2*31*24*60*60)
)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sResponse = curl_exec ($ch);
Run Code Online (Sandbox Code Playgroud)
目标系统上的文件似乎是空的.
bar*_*iir 40
实际上我在开始提问时找到了答案.在PHP 5.5中有一个包含curl的新变量:默认情况下,它在PHP 5.5中CURLOPT_SAFE_UPLOAD设置,false并切换到truePHP 5.6中的默认值.
这样可以防止"@"上传修饰符出于安全原因而工作 - 用户输入可能包含恶意上传请求.您可以CURLFile在CURLOPT_SAFE_UPLOAD设置为true或时使用该类上传文件(如果您确定您的变量是安全的,您可以手动切换CURLOPT_SAFE_UPLOAD到false):
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
Run Code Online (Sandbox Code Playgroud)
以下是让我搜索正确方向的信息来源:http://comments.gmane.org/gmane.comp.php.devel/87521
在改变后的功能中也提到了这一点:http://php.net/manual/en/migration56.changed-functions.php 但不是在落后的不兼容的变化中,真的让我失望......
小智 29
只需对PHP 5.5或更高版本进行以下更改
而"@" . $localFile不仅仅是使用new \CURLFile($localFile)
并设置
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
Run Code Online (Sandbox Code Playgroud)
包括运行时检查以使您的代码与较低版本兼容,如下所示
$aPost = array(
'default_file' => 'html_version.html',
'expiration' => (2*31*24*60*60)
)
if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
$aPost['file'] = new CURLFile($localFile);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
} else {
$aPost['file'] = "@".$localFile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sResponse = curl_exec ($ch);
Run Code Online (Sandbox Code Playgroud)