Here you are, with two totally different approaches to getting users’ images into your database (or at least the paths to those images). In fact, you’ve probably spent as much time working through this code as any other code you’ve run across in your PHP journey. Now, one question begs to be answered: which approach is best?
The most accurate answer to that is, “It depends.” Or maybe, “It’s up to you.” Those are frustrating answers and probably completely dissatisfying. That’s because the sort of questions you’re getting into-storing images or handling errors or interacting with other frameworks and other people’s code-you’re not always going to have clear “right” answers.
For example, you have to consider questions like the following: Do you have a particularly small file system with which to work? Are you charged based on the space your web server’s files take up? Is that charge greater or lesser than the charges you’re assessed for the size of your database? Is your database locally accessible and blistering fast? Or, is it a slow connection to another machine?
Yet, at the end of the day, you sometimes have to say, “I’m not sure just like this approach-better or that approach better.” That’s okay. You might just need to pick something, try it out, and get moving. There are plenty of cases in which the only real solution is to wait around analyzing the options for hours (days! weeks!) instead of moving forward.
OK, If You Insist on an Answer …
If you’re not sure, store your images on a file server, and store just the path to that image in your database. The fact is, although you can write good code that both stores an image in a database and displays that image, it’s a lot tougher to do things right. Every time a SELECT runs against your images table and grabs the contents of the image_data column, you’re selecting the entire size of that image’s data. You might have 100 rows each with an image of an average size of 1MB and 100 MB of image data clogging up your network and database traffic. When in doubt, you’ll probably stick with a path in your database, like the example in Chapter 9. But now, you have a firm handle on just what goes on with images, whether they’re stored in the database or not.
Back on the Path
All things being equal, going with images stored on the file system is the better solution. (To be clear, though, all things are never equal!) Because that’s a good default option, the examples in the rest of this book will assume that’s your setup. So how do you get back to that solution? First, you should have backed up your scripts. I(you didn’t, you might want to re-download the sample files again from the Missing CD page (www.phphelponline.com) and use the versions that don’t store images in the database.