Hey, trying to post a file using curl and all is working great. I have one problem. I can't declare my file outside of my post_file() function. I call this function in my application many times so want it to be reusable.
So this works:
function call_me(){
$file_path = "/home/myfile.mov";
$url = "http://myurl.com";
$this->post_file($url, $file_path);
}
function post_file($url, $file_path){
$data['Filedata'] = "@".$file_path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
return $response;
}
However this does not:
function call_me(){
$file_path = "/home/myfile.mov";
$url = "http://myurl.com";
$data['Filedata'] = "@".$file_path;
$this->post_file($url, $data);
}
function post_file($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
return $response;
}
Any ideas? Cheers.
Filedata
),而 #2 允许任何名称 - 如果接收代码假定文件将以某个名称传递,则 #2 可能会轻易地发送文件,但随后被忽略。print_r($data);
显示了什么? - Rudu