问题描述
我的 cURL 上传在从 PHP 5.5 升级到 5.6 后失败:
I've got a cURL upload that fails after upgrading from PHP 5.5 to 5.6:
$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.
推荐答案
其实我在开始提问的时候就找到了答案.PHP 5.5 中的 curl 包含一个新变量:CURLOPT_SAFE_UPLOAD
在 PHP 5.5 中默认设置为 false
并切换为默认值 true
代码>在 PHP 5.6 中.
Actually I found the answer while starting the question. There is a new Variable included with curl in PHP 5.5: CURLOPT_SAFE_UPLOAD
this is set to false
by default in PHP 5.5 and is switched to a default of true
in PHP 5.6.
出于安全原因,这将阻止@"上传修饰符工作 - 用户输入可能包含恶意上传请求.您可以使用 CURLFile
类上传文件,而 CURLOPT_SAFE_UPLOAD
设置为 true
或(如果您确定您的变量是安全的,您可以切换手动将 CURLOPT_SAFE_UPLOAD
改为 false
):
This will prevent the '@' upload modifier from working for security reasons - user input could contain malicious upload requests. You can use the CURLFile
class to upload files while CURLOPT_SAFE_UPLOAD
is set to true
or (if you're sure your variables are safe you can switch the CURLOPT_SAFE_UPLOAD
to false
manually):
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
以下是让我朝着正确方向搜索的信息来源:http://comments.gmane.org/gmane.comp.php.devel/87521
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://php.net/manual/en/migration56.changed-functions.php但不是在向后不兼容的变化中,真的把我绊倒了......
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...
这篇关于从 PHP 5.5 升级到 5.6 后,cURL 文件上传不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!