0

I am working on a Social Network application with Codeigniter 3, Ion-Auth and Bootstrap 4. You can see the Github repo HERE.

When editing a user's profile, I check if there is a new user photo (avatar) in the edit form. If it is, I use it, if not I use (keep) the one already existing in the users table (file path is application/controllers/Auth.php):

$new_file = $this->upload->data('file_name');
$this->file_name = (isset($new_file) && !empty($new_file)) ? $new_file : $user->avatar;

The above code works fine.

However, I need update the user's photo within the session and for this purpose I added just below $this->file_name = (isset($new_file) && !empty($new_file)) ? $new_file : $user->avatar:

if (isset($new_file) && !empty($new_file)) {
    $this->session->userdata('user_avatar') = $new_file;
}

The above if statement causes the error "Can't use method return value in write context".

What am I doing wrong?

Razvan Zamfir
  • 4,209
  • 6
  • 38
  • 252
  • 2
    It's probably that `userdata()` returns a value, you may need to find a corresponding method to write back the data. – Nigel Ren Sep 06 '20 at 07:29

2 Answers2

1

See Why check both isset() and !empty() regarding the use of isset and empty. You do not need to use both.

if (isset($new_file) && !empty($new_file)) {
    $this->session->userdata('user_avatar') = $new_file;
}

Here you should be using $this->session->set_userdata('user_avatar',$new_file) ( as per the user guide).

So it becomes

if (!empty($new_file)) {
    $this->session->set_userdata('user_avatar',$new_file);
}

You do not need to even consider using isset() as you are defining $new_file in your code above, so it will always be "set" i.e isset($new_file) will always be true.

But what do you intend to do in the case where $new_file is empty? That's a question you need to consider.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
0

You usually get that error in an if statement calling a method that performs write.