1

Is it possible to upload the picture and set it as user picture?

User picture can be set via $user->picture = $file->fid; user_save($user);but how do I upload the file from a URL to the public:// directory?

1
  • Programmatically using URL (title), or using uploaded picture (question)?
    – Citricguy
    Commented Sep 28, 2012 at 4:16

1 Answer 1

1

If you want to create the code for saving the image yourself, you could do

$response = drupal_http_request($url_image);

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

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

6
  • This will probably work, but if the user enters url-to-a-vey-lar.ge/file.png , that costs php memory.
    – AKS
    Commented Sep 28, 2012 at 10:30
  • I'd like to know a better way, then. Always wanting to learn something new :)
    – m0d
    Commented Sep 28, 2012 at 11:40
  • looks good, but where are $filename from?
    – Luciuz
    Commented Sep 28, 2012 at 16:53
  • @m0d you can use curl (if installed) or even copy(newerphp versions only, requires some changes in php.ini that maybe insecure).
    – AKS
    Commented Sep 28, 2012 at 17:43
  • @Luciuz You set it. It includes the filename you want to use for saving the file.
    – m0d
    Commented Oct 1, 2012 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.