Curl PHP File Upload

nyunyu

New Member
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:\[code\]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;}\[/code\]However this does not:\[code\]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;}\[/code\]Any ideas? Cheers.
 
Back
Top