dj-kenpo Posted July 8, 2007 Share Posted July 8, 2007 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! Quote Link to comment Share on other sites More sharing options...
trq Posted July 8, 2007 Share Posted July 8, 2007 I'm no expert here but one would think that using file_exists would probably be the quicker / easier method given your needs. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Share Posted July 8, 2007 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) Quote Link to comment Share on other sites More sharing options...
per1os Posted July 8, 2007 Share Posted July 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 8, 2007 Share Posted July 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
dj-kenpo Posted July 8, 2007 Author Share Posted July 8, 2007 hmmm but by that logic then why store it in the db at all? if you're checking if the file exists, why do a first look up? we already know the name "$user_ID.jpg". not sure if I see the logic mjdamato Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 8, 2007 Share Posted July 8, 2007 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.