Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Storing Images In MySQL

Note: This article has been replaced by Storing Images in MySQL Revisited (by Quentin Zervaas, 2010). We've left this article here for historical purposes, but we strongly recommend you use the newer article instead.

Uploading The Image

Here we get funky and can stick the image into the database.

As with all user input it is vital that it is checked with some sort of sanity checking to be sure we are getting what we asked for.

In this case, we are asking for image files of types that are supported by getimagesize().

First of all, we need to check that when somebody accesses our page the they have POSTed a file. If they have not, we can echo a simple message and if they have, we can call the upload() function that we will create. Please note that this function uses PHP5 exceptions.

Listing 4 listing-4.php
    // check if a file was submitted
    if(!isset($_FILES['userfile'])) {
        echo '<p>Please select a file</p>';
        try {
            // give praise and thanks to the php gods
            echo '<p>Thank you for submitting</p>';
        catch(Exception $e) {
            echo $e->getMessage();
            echo 'Sorry, could not upload file';

In This Article

Article History

Apr 20, 9804
Initial article version
Jan 18, 2008
Updated listing 5 to not include non-existent conf.php