"使用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

相關文章
相關標籤/搜索