How do I upload an image to the public:// directory?
If you want to create the code for saving the image yourself, you could do

$response = drupal_http_request($url_image);

if ($response-&gt;code == 200){
  $file = file_save_data($response-&gt;data, 'public://' . 'foldername/' . $filename);
}


And in $file->fid you'll find the fid you need.