drupal - How do I programmatically delete user profile picture? -
I have the following code to upload a program image in an existing user account (which I have found on the internet). I'm still getting the " Duplicate entry " error
So I thought I should remove the existing photo first (if any) before uploading the first picture ) Or is there a better way to overwrite without getting the error?
DOException: SQLSTATE [23000]: Integrity Constraints: 1062 For Duplicate Entry '' Key 'Yuri': {file_managed} (UID, Yuri , Filememes, files, status, timestamp, type) values ((db_insert_placeholder_0 ,: db_insert_placeholder_1 ,: db_insert_placeholder_2 ,: db_insert_placeholder_3 ,: db_insert_placeholder_4 ,: db_insert_placeholder_5 ,: db_insert_placeholder_6); Array ([: db_insert_placeholder_0] => 1302 [: db_insert_placeholder_1] => [: db_insert_placeholder_2] => image / png [: db_insert_placeholder_3] => 143769 [: db_insert_placeholder_4] => 1 [: db_insert_placeholder_5] => 1414500789 [: db_insert_placeholder_6] = > Image) in drupal_write_record () (line 7202 /home1/public_html/example/includes/common.inc).
Here's the code I am using:
$ obj_tochange = user_load ($ user); $ Image_path = 'http: //www.example.com/sites/default/files/photo_gallery/newphoto.png'; $ Image_info = image_get_info ($ image_path); // Create a file object $ file = new stud class (); $ File- & gt; Uid = $ obj_tochange- & gt; Uid; $ File- & gt; Yuri = $ image_path; $ File- & gt; Filemime = $ image_info ['mime_type']; $ File- & gt; Position = FILE_STATUS_PERMANENT; $ File- & gt; Filesize = $ image_info ['file_size']; File_save ($ file); $ Edit ['picture'] = $ file; User_save ($ obj_tochange, $ edit);
You need to copy the file before calling. If it matches any other file on the server, then it should be automatically converted to file path, so it will not throw away this error.
$ file = new StdClass (); // This is $ file = file_copy ($ file, 'public: //', FILE_EXISTS_RENAME) as your code; File_save ($ file);
Comments
Post a Comment