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)
        <title>Upload an Image</title>
            <h1>Upload an Image</h1>
                <a href="./">View uploaded images</a>
            <form method="post" action="process.php" enctype="multipart/form-data">
                    <input type="file" name="image" />
                    <input type="submit" value="Upload Image" />

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