Jump to content

any php gurus?


dj-kenpo

Recommended Posts

just wondering what's faster, an sql query or physiclly looking checking if a file exists.

 

exact situation:

 

user image icon for a forum.

 

is it faster to check if $user_id.jpg exists, if so show it, if not sow generic icon.

 

or, store filenames in an sql table and query for the filename under that user_id and then display?

 

 

I have no need for multiple images for the same person, which is where sql would make it better, I'm just wndering how slow or fast it is to phyiscly look for a file WHILE the script is generating. I'd imagine it might be fast as includes are fast right? and that's just file etc stuff....

 

thanks!

Link to comment
Share on other sites

If you're pulling data about someone from a table to make a page then it wouldn't hurt to pull out an extra piece of data holding a filename. You wouldn't notice any speed difference to worry about.

 

Checking for the existence of a file means you're getting PHP to do two completely unrelated things (pull user data from table AND check for a file)

Link to comment
Share on other sites

The best way to go about doing it is storing the file information in the database that way when the user is in you can locate the file and the file does not have to be stored in separate folders for users etc and this way if you want to expand it makes i easy. I would also suggest creating a hash for the filename or some type of combination as to avoid duplicates, and as long as that data is stored in the DB you are set.

 

MySQL I think would be faster if you already have the connection present etc. Either way it will be pretty fast whichever method you choose.

Link to comment
Share on other sites

Well, I have a different opinion. I agree that you should pull the user image from the database, but you still should check if the file exists! Things happen and files get deleted, folders renamed, etc. A web application should never assume that a certain condition exists, it should be able to handle errors gracefully.

 

So, I would save the file name int he database, but then also check that the image exists. If the image doesn't exist, then show the default image. Of course, if your defaut image doesn't exist you are SOL.

Link to comment
Share on other sites

You already have to do a lookup in the database to get the other user data anyway, right? So, there is no extra effort in that regard. Storing it in the database isn't a requirement, but if you decide not to, then you are locked into having to name the images the same as the user id/name. Storing it in the database gives you more flexibility.

 

What I am trying to stress is that even if you do store it in the database when the file is saved you cannot rely upon the fact that the image exists at some later point in time. Much better to take the .0002 seconds to validate the image exists and show a default image if it doesn't (rather than a missing image placeholder).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.