Load Question: file_exists versus MySQL query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Load Question: file_exists versus MySQL query

Post by JAB Creations »

Which creates less load, file_exists or a MySQL query?

I figure if I execute file_exists for a single file type (GIF) versus executing it three times (GIF, JPG, PNG) it'll definitely create less load then a MySQL query. I could restrict avatar file types or simply convert them to GIF format...not sure.

As I'm only just starting to program my very first generation of PHP/MySQL based membership script (sort of a test version if you will) I'm not sure how often this will occur; I'm not sure if I will need to call a query independently if there is no other data I need to fetch from the MySQL database. I'm thinking there isn't though I could be wrong.

So worst case scenario is I think I'll always fetch more then one column/row and thus should stick the file extension in the database (only three letters for the extension itself). I'd really like to take this sort of thing in to consideration now to get in to good coding practices as I learn more. Thoughts?

Code: Select all

$image_avatar_gif = 'upload/'.get_id_length($row['id']).'_'.$row['username'].'_avatar.gif';
$image_avatar_jpg = 'upload/'.get_id_length($row['id']).'_'.$row['username'].'_avatar.jpg';
$image_avatar_png = 'upload/'.get_id_length($row['id']).'_'.$row['username'].'_avatar.png';
 
if (file_exists($image_avatar_gif)) {echo '<img class="left margin" src="'.$image_avatar_gif.'" />';}
else if (file_exists($image_avatar_jpg)) {echo '<img class="left margin" src="'.$image_avatar_jpg.'" />';}
else if (file_exists($image_avatar_png)) {echo '<img class="left margin" src="'.$image_avatar_png.'" />';}
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Load Question: file_exists versus MySQL query

Post by califdon »

If I understand your question, I would think it would normal to include the full filename, including the extension, in the table. Why wouldn't you? Don't worry about the extra 4 bytes unless you are expecting to store hundreds of millions of rows! There are far more critical issues to pay attention to than 4 bytes per row.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Load Question: file_exists versus MySQL query

Post by Weirdan »

From my experience, situations where you need to show only user's avatar and nothing of his other info are very rare. And if you're going to select user's data from database, why not select his avatar's filename as well?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Load Question: file_exists versus MySQL query

Post by JAB Creations »

This is a load question. :|

Didn't see the second reply when I posted...

I'm trying to find a nice balance between minimal load and seeing what I can do to funnel to that goal. For example If a user uploads an avatar it could be a GIF, JPG, or PNG file. In that instance I could convert all supported file types to GIF and not even have to do half of the above code.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Load Question: file_exists versus MySQL query

Post by califdon »

I think I see what you're driving at. Instead of arbitrary filenames for avatar images submitted by users, you want to name each one the same as its user's username, but with the possibility of 3 file types. You could do that, although I'm not sure why that would be at all important. I would simply store the filename, including extension, in the user's record, as Weirdan said. I think you are worrying about something that is inconsequential, either in terms of storage or page loading time.
Post Reply