title: Curl PHP File Upload
body: <p>
  
  <p>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.</p>
  
  <p>So this works:</p>
  
  <pre><code>function call_me(){
      $file_path = "/home/myfile.mov";
      $url = "http://myurl.com";
      $this-&gt;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></pre>
  
  <p>However this does not:</p>
  
  <pre><code>function call_me(){
      $file_path = "/home/myfile.mov";
      $url = "http://myurl.com";
      $data['Filedata'] = "@".$file_path;
      $this-&gt;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></pre>
  
  <p>Any ideas? Cheers.</p>