0

I have a form that allows users to change their account settings, including their profile picture. Everything works fine, but if the user tries to save the settings without loading any images then the array to string conversion in error occurs.

If I do var_dump($attachment_id); I get object (WP_Error) #25169 (3) {["errors"] => array(1) {["upload_error"] => array(1) {[0] => string(21) "No file was uploaded. " }} ["error_data"] => array(0) {} ["additional_data": protected] => array (0) {}}

What am I doing wrong ?

Array to string conversion in: update_user_meta($user_id, 'image', $_FILES ['image']. ":". $attachment_id->get_error_message ());

This is where the error occurs

if ( is_wp_error( $attachment_id ) ) {
      // The error occurs on the next line (update_uder_meta)
      update_user_meta( $user_id, 'image', $_FILES['image'] . ": " . $attachment_id->get_error_message() ); 

    }

This is the complete code

// Save Avatar Action
function action_woocommerce_save_account_details( $user_id ) {  
  if ( isset( $_FILES['image'] ) ) {
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    function wp_set_custom_upload_folder($uploads) {
        $uploads['path'] = $uploads['basedir'] . '/avatar-upload';
        $uploads['url'] = $uploads['baseurl'] . '/avatar-upload';    
        if (!file_exists($uploads['path'])) {
          mkdir($uploads['path'], 0755, true);
        }
        return $uploads;
    }
    add_filter('upload_dir', 'wp_set_custom_upload_folder');    
      
    $attachment_id = media_handle_upload( 'image', 0 );

    if ( is_wp_error( $attachment_id ) ) {
      // The error occurs on the next line (update_uder_meta)
      update_user_meta( $user_id, 'image', $_FILES['image'] . ": " . $attachment_id->get_error_message() ); 

    } else {
      $old_attachment_id = get_user_meta( $user_id, 'image', true );
      wp_delete_attachment($old_attachment_id);
      update_user_meta( $user_id, 'image', $attachment_id );
    } 
    remove_filter('upload_dir', 'wp_set_custom_upload_folder');
  }

}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
Snorlax
  • 183
  • 4
  • 27
  • 1
    Do a `var_dump($_FILES)` and check what it actually contains. My bet is that `$_FILES['image']` is an array, which causes your script to error out when you're trying to concatenate it with a string. Not really sure what you expect to get from `$_FILES['image']`? – M. Eriksson Aug 20 '22 at 16:39
  • Contain this: `array(1) { ["image"]=> array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) } }` – Snorlax Aug 20 '22 at 16:43
  • 1
    `["image"]=> array(5) {....}` says it all. `$_FILES['image']` is an array, not a string so you can't concatenate it with strings. – M. Eriksson Aug 20 '22 at 16:44
  • Forgive me but I'm new to all of this. Does this mean that 'image' contains 5 values and I am not calling anyone ? – Snorlax Aug 20 '22 at 16:45
  • 1
    It means that `$_FILES['image']` contains an array with 5 elements. If you just do `$_FILES['image']`, you will access that array. If you want to access some specific element in that sub-array, you need to do: `$_FILES['image']['name']` and so on (change `name` to the element you want). https://www.php.net/manual/en/language.types.array.php – M. Eriksson Aug 20 '22 at 16:49
  • Thanks for your comments, I appreciate. I have stumbled upon this post before, but I was unable to fix the problem on my own. As I am learning all this now I am having a bit of a hard time figuring out what I should actually do with my code. – Snorlax Aug 20 '22 at 16:50
  • Ok, I think I understand. So I changed it like this: `$_FILES['image']['error']`, now I'm not getting any errors. The element ['error'] is present in the array. Am I doing the right thing? – Snorlax Aug 20 '22 at 16:56
  • 1
    Should work. Why not just test it and see? – M. Eriksson Aug 20 '22 at 16:59
  • Tried, and everything works fine. Even when the user doesn't upload any images, if I hit the save button, everything works as it should. But as I am new I am very insecure about the things I do and I don't know if what I do is actually right. – Snorlax Aug 20 '22 at 17:01
  • 1
    Well, I don't use Wordpress so I can't speak for that part of the code. I would say, if you have tested it properly and it works as expected, then it's probably "good enough" for now. – M. Eriksson Aug 20 '22 at 17:19
  • I thank you for everything. Is there no way to accept the question as resolved? In this case there is no answer, only comments. How can it be done ? – Snorlax Aug 20 '22 at 17:20
  • 1
    The question has already been closed as a duplicate, so we're all set! :-) – M. Eriksson Aug 20 '22 at 17:27

0 Answers0