How do I upload an image to the public:// directory? Is it possible to upload the picture and set it as user picture?

User picture can be set via $user-&gt;picture = $file-&gt;fid; user_save($user);but how do I upload the file from a URL 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.