"使用curl 上传打印$_FILES提示空数组"的解决方法

PHP版本大于5.6的时候请使用 CURLFile 发送进行发送,要否则在打印时会报 *$_FILES()*为空数组php

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/test/doAction.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); //  PHP 5.6.0 后必须开启
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'file' => new CURLFile(realpath('C:\wamp64\www\test\bing.jpg')),
]);

curl_exec($ch);
?>
  • doAction.php
<?php

  header("Content-type:text/html;charset=utf-8");

  print_r($_FILES);

 ?>

参考: php5.6使用cURL上传文件html

PHP官方cURL文档segmentfault

相关文章
相关标签/搜索