从 PHP 5.5 升级到 5.6 后,cURL 文件上传不再起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25934128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:03:56  来源:igfitidea点击:

cURL file uploads not working anymore after upgrade from PHP 5.5 to 5.6

phpcurlphp-5.6

提问by bardiir

I've got a cURL upload that fails after upgrading from PHP 5.5 to 5.6:

我有一个从 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);

The file seems to be empty on the target system.

该文件在目标系统上似乎为空。

回答by bardiir

Actually I found the answer while starting the question. There is a new Variable included with curl in PHP 5.5: CURLOPT_SAFE_UPLOADthis is set to falseby default in PHP 5.5 and is switched to a default of truein PHP 5.6.

实际上,我在开始提问时找到了答案。PHP 5.5 中的 curl 包含一个新变量:CURLOPT_SAFE_UPLOADfalse在 PHP 5.5 中默认设置为,true在 PHP 5.6 中切换为默认值。

This will prevent the '@' upload modifier from working for security reasons - user input could contain malicious upload requests. You can use the CURLFileclass to upload files while CURLOPT_SAFE_UPLOADis set to trueor (if you're sure your variables are safe you can switch the CURLOPT_SAFE_UPLOADto falsemanually):

出于安全原因,这将阻止“@”上传修饰符工作 - 用户输入可能包含恶意上传请求。您可以使用CURLFile该类在CURLOPT_SAFE_UPLOAD设置为trueor 时上传文件(如果您确定您的变量是安全的,您可以手动切换CURLOPT_SAFE_UPLOADfalse):

 curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

Here's a source for the information that got me searching in the right direction: http://comments.gmane.org/gmane.comp.php.devel/87521

这是让我朝着正确方向搜索的信息来源:http: //comments.gmane.org/gmane.comp.php.devel/87521

It's mentioned in the changed functions too: http://php.net/manual/en/migration56.changed-functions.phpBut not in the backward incompatible changes, really tripped me off...

它也在更改的功能中提到:http: //php.net/manual/en/migration56.changed-functions.php但不是在向后不兼容的更改中,真的让我失望了......

回答by Deepti Gehlot

Just do following changes for PHP 5.5 or greater

只需对 PHP 5.5 或更高版本进行以下更改

Instead of "@" . $localFilejust use new CurlFile($localFile)

而不是"@" . $localFile仅仅使用new CurlFile($localFile)

And set

并设置

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);

回答by Simhachalam Gulla

Include a runtime check to make your code compatible with lower versions too like below

包括运行时检查以使您的代码与较低版本兼容,如下所示

$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);