PhpRiot
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 Revisited

The Upload Form

Now that we've created a database table in which to store images and written code to connect to the database, let's create an upload form so users can upload images. To keep things as simple as possible, I'm only going to include a single field in this form.

In order to accept uploaded files from a form, you must add the enctype attribute to the HTML <form> tag. The value of this attribute must be multipart/form-data, which indicates that binary data will be uploaded.

Including this attribute means the PHP superglobal $_FILES will be populated with information about the uploaded file. We'll look at this more closely soon.

In this example, the upload form will be stored in a file called upload.php. It will post its values to a file called process.php.

Listing 5 The image upload form (upload.php)
<?php
    require_once('globals.php');
?>
<html>
    <head>
        <title>Upload an Image</title>
    </head>
    <body>
        <div>
            <h1>Upload an Image</h1>
 
            <p>
                <a href="./">View uploaded images</a>
            </p>
 
            <form method="post" action="process.php" enctype="multipart/form-data">
                <div>
                    <input type="file" name="image" />
                    <input type="submit" value="Upload Image" />
                </div>
            </form>
        </div>
    </body>
</html>

Later in this article we will create a script that lists out all uploaded images. The link at the top of this upload form points to that script.

In This Article



Bonus listings: 1 available