如何通过curl发送$ _FILES

时间:2016-07-03 22:41:02

标签: php file curl

我尝试通过$_FILES发送curl

$post = array('files' => $_FILES);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.com/upload_page');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec ($ch);
curl_close($ch);

但我注意到行curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

  

注意:数组转换为字符串

我知道$post应该是字符串,但它是如何为$_FILES做的?

1 个答案:

答案 0 :(得分:1)

以下是使用php

卷曲上传图片的代码
$file_name_with_full_path = realpath('./sample.jpeg');

$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);