我正在尝试通过curl在另一台服务器上上传文件。我为此创建了一个脚本,但是无法获取$_FILES
参数。它是空的。
$request = curl_init('http://localhost/pushUploadedFile.php');
$file_path = $path.$name;
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '@' . $file_path,
'test' => 'rahul'
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);exit();
pushUploadedFile.php:
print_r($_FILES['file']);
您正在使用哪个版本的PHP?在PHP 5.5中,CURLOPT_SAFE_UPLOAD
引入了curl选项,该选项的初始默认版本true
为PHP 5.6.0。如果是,则true
使用@/path/to/file
禁用文件上传。因此,如果您使用的是PHP 5.6或更高版本,则必须将其设置false
为允许上传:
curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);
但是自@/path/to/file
PHP 5.5.0起,上传的格式已经过时和不赞成使用,您CurlFile
现在应该使用该类:
$request = curl_init();
$file_path = $path.$name;
curl_setopt($request, CURLOPT_URL, 'http://localhost/pushUploadedFile.php');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => new CurlFile( $file_path ),
'test' => 'rahul'
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句