我正在使用Codeigniter并尝试将文件发送到另一台服务器,但是当我使用new CURLFile()
来创建文件时,屏幕上什么也没有显示,也没有发送文件。
这是我的代码:
Testing.php
public function sendFile(){
$ch = curl_init();
$cfile = new CURLFile(base_url().'assets/upload/RAD PAPER.doc');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => $cfile, 'filename'=>'name'));
curl_setopt($ch, CURLOPT_URL, 'http://localhost/devel/api/receive');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_exec($ch);
curl_close($ch);
}
并在另一台服务器中:
Api.php
public function receive(){
$folder = base_url().'assets/';
$path = $folder . $_FILES['file']['name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
}
echo "smthg to print";
print_r($_FILES['file']);
}
似乎永远不会调用函数receive。但是当我更改这一行时:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => $cfile, 'filename'=>'name'));
至:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => 'thisisstring', 'filename'=>'name'));
在Api.php中向我返回消息“要打印的消息”,但$ _FILES ['file']未定义,因为它不是文件。我的代码有什么问题?使用CURLFile是否会出错?我也尝试curl_file_create()
。