title: PHP Curl Image Upload
body: <p>How to achieve an image upload via cURL?</p>
  
  <p>I was told to use curl_file_create but it's not working.</p>
  
  <p>This is the array from post when I manually (using Chrome) upload a file and see the raw structure:</p>
  
  <pre><code>Array
  (
      [image] =&gt; Array
          (
              [0] =&gt; 377529090726537.jpg
          )
  
      [thumbnail_digest] =&gt; Array
          (
              [0] =&gt; EMPTYDIGEST
          )
  
      [digest_present] =&gt; Array
          (
              [0] =&gt; 0
          )
  
      [image_rotation] =&gt; Array
          (
              [0] =&gt; 0
          )
  )
  </code></pre>
  
  <p>Inside my cURL I tried:</p>
  
  <pre><code>$cfile = curl_file_create('IMG_33071.jpg','image/jpeg','test_name');
  $post_data = array(
  'image' =&gt; $cfile,
  );
  </code></pre>
  
  <p>But after running the script, I receive this error:</p>
  
  <blockquote>
    <p>Catchable fatal error: Object of class CURLFile could not be converted
    to string in C:\Path\to\my\server\curl.php on line 144</p>
  </blockquote>
  
  <p>The image is on the same folder from curl.php file.
  My php version is 5.5.</p>
  
  <p>I also tried this:</p>
  
  <pre><code>$cfile = curl_file_create('http://www.siteexample.com/path/to/my/file.jpg','image/jpeg','test_name');
  </code></pre>
  
  <p>But no success :(</p>
  
  <p>My structure:</p>
  
  <pre><code>//These are the post data username and password
  $login_data = 'email='.$email.'&amp;password='.$password.'&amp;entrar=Entrar';
  
  //Create a curl object
  $login = curl_init();
  
  //Set the useragent
  $agent = $_SERVER["HTTP_USER_AGENT"];
  //$agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
  curl_setopt($login, CURLOPT_USERAGENT, $agent);
  
  curl_setopt($login, CURLOPT_SSL_VERIFYPEER, false);
  
  //Set the URL
  curl_setopt($login, CURLOPT_URL, $login_url );
  
  //This is a POST query
  curl_setopt($login, CURLOPT_POST, 1 );
  
  //Set the post data
  curl_setopt($login, CURLOPT_POSTFIELDS, $login_data);
  
  //We want the content after the query
  curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
  
  //Follow Location redirects
  curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
  
  /*
  Set the cookie storing files
  Cookie files are necessary since we are logging and session data needs to be saved
  */
  
  curl_setopt($login, CURLOPT_COOKIEJAR, 'cookie.txt');
  curl_setopt($login, CURLOPT_COOKIEFILE, 'cookie.txt');
  
  //Execute the action to login
  $LoginResult = curl_exec($login);
  echo $LoginResult;
  
  
  
      //create array of data to be posted
  $post_data = array(
  'name' =&gt; 'Name',
  'email' =&gt; $email,
  'email_confirm' =&gt; $email,
  'phone' =&gt; '9999999999',
  'passwd' =&gt; $password,
  'passwd_ver' =&gt; $password,
  'create' =&gt; '',
  );
  
  
  //traverse array and prepare data for posting (key1=value1)
  foreach ( $post_data as $key =&gt; $value) {
      $post_items[] = $key . '=' . $value;
  }
  
  
  //create the final string to be posted using implode()
  $post_string = implode ('&amp;', $post_items);
  
  var_dump($post_string);
  
  curl_setopt($login, CURLOPT_URL, 'http://urltocurl.com/verify');
  curl_setopt($login, CURLOPT_POST, false);
  curl_setopt($login, CURLOPT_POSTFIELDS, $post_string);
  curl_setopt($login, CURLOPT_TIMEOUT, 0);
  $answer = curl_exec($login);
  echo $answer;
  
  
  if (curl_error($login)) {
      echo curl_error($login);
  }
  
  //close the connection
  curl_close($login);
  </code></pre>
  
  <p>Now take a look at the array at the beggining.</p>
  
  <pre><code>Array
      (
          [image] =&gt; Array
              (
                  [0] =&gt; 377529090726537.jpg
              )
  </code></pre>
  
  <p>It is an array inside another one. How to achieve that? Maybe curl_file_create is working but it's sending the image to a wrong location of array.</p>