Welcome Guest, Not a member yet? Register   Sign In
What is better, db check of file_exists?
#1

[eluser]bapobap[/eluser]
Hi all,

I was wondering if I could get some advice, I can't see any problems with either but maybe I'm missing something.

Basically, users can have a profile picture. Once they have uploaded a profile picture, I can output an icon version of their profile picture in the header of the HTML.

To check if they have one or not, I'm checking if the file exists. If it does, it gets shown, if not, a default pic is shown.

I was wondering, would it be better to have this check stored in the db, as part of the user profile. This means one more db call.

So which is better, doing a file_exists every time a page is loaded, or a db call?

Thanks!
#2

[eluser]Michael Wales[/eluser]
The way I envision things it really wouldn't be another DB Call. You are probably already storing the user's record (to display information on the page) and the filename would be part of that user's record. Simply checking whether the corresponding variable is empty or not would suffice.
#3

[eluser]bapobap[/eluser]
Would I just set the filename in the session then, when the session is created on login?
#4

[eluser]Michael Wales[/eluser]
I'm assuming a lot here, but let's say this is your users table:
Quote:id
username
email
password
first_name
last_name
email
avatar
created_on

I assume you are displaying some of this information on your pages (you are pulling the user's record down to display within the view):
Code:
$query = $this->db->getwhere('users', array('id'=>$id));
  if ($query->num_rows() > 0) {
    $this->data->user = $query->row();
  }

Then to check if they have uploaded an avatar image simply:
Code:
if (!empty($this->data->user->avatar)) {
    // We have a filename in this field, no need for another database call because we were
    // already storing the user's record from information needed previously.
  } else {
    // They haven't uploaded an avatar - show the default
  }

Like I said, this is a lot of assumption, but - you don't have to make one database call per piece of information. If you are already pulling down the user's information (and I don't know how you would create a website with login functionality without doing so) then just modify that query/use it's return information for this functionality.

To directly answer your question - file_exists() or a database call. It doesn't really matter - I would assume file_exists() is faster, but if you already have the database information (like I describe above), using that information you already have will be faster.
#5

[eluser]bapobap[/eluser]
I hadn't stored all the users information in the session, just their id and their username, which is all that is needed in 90% of cases. If they need to change their password etc, I just get that out of the db using the user id. I thought it was best to store as little as required in the session.

Thanks
#6

[eluser]Michael Wales[/eluser]
hmm - well, if you always know the filename then sure, file_exists() is a good option (say, if you base the filename off of their username). If you don't do something like that, then a database call is your only option.

It is a good idea to store very little in the session. Personally, I only store an encrypted user ID, then make a call to the database within my Controller sub-class.




Theme © iAndrew 2016 - Forum software by © MyBB