Inserting a Raw Image into a Table PHP Help

It’s time to revisit create_users.php. You’re going to use a lot of your existing code, but there are also some changes to make. All of the checks you’ve put in place to ensure that your user uploaded a valid image, that no errors were generated by the server or PHP, and that the file is an image via getimagesize are just fine.

Where things change is in the section of code that you used to move the temporary image into a final location (page*286). In this approach, the final location is the images table, so you must replace that code.

Here’s the create_user.php script with the path code removed.

Inserting a Raw Image into a Table

Your code remains substantially the same. The big change is that now you need a new INSERT statement. and this statement doesn’t insert into users, but into images.

Here’s the beauty of this solution, though: you can get every bit of the information you need to put into images from the $FILES array (which is actually an array of arrays):

Inserting a Raw Image into a Table

There’s a lot going on here, and some of it is flat-out confusing, so take this code piece by piece.

First, this code creates a new $image variable that’s actually just for convenience:

$image = $_FILES[$image_fieldname];

This variable makes it easier to deal with all the properties of an image at once. You don’t have to continually type $FILES[$image _fieldname], over and over. This step isn’t necessary, but it does make things much more convenient.

Next. you can get the name of the image from this array:

$image_filename = $image[‘name’];

Beware: getimagesize Doesn’t Return a File Size

Here’s where things start to get a little weird. Despite its name, getimagesize does return a numeric file size of the uploaded image. Rather, it returns an array of information about the image such as its MIME type (which you need) and the height and width of the image that you might use to display the image in an HTML page (which you don’t currently need).

This might lead you to believe that you should do something like this:

$image_size = getimagesize($image[‘tmp_name’]);

In fact, that’s a problem on two counts: getimagesize returns an array, not a size, and the sizes that getimagesize returns in that array are height and width, not file size.

What you do need from the returned array, though, is the MIME type: image_info = getimagesize($image[‘tmp_name’]);
$image_mime_type = $image_info[‘mime’];

You also still need the actual file size of the uploaded image. You can get that from a property on the original image-related array:

$image_size = $image[‘size’];

The file _get_contents Function Does What You Think It Does

Sometimes a function’s name is a bit misleading, such as you just learned with getimagesize. Other times, a function is perfectly named; that’s the case with file _get_contents. This function retrieves an object’s data in binary form, which is just what you want for the image_data column in your image table:
$image_data = file_get_contents($image[‘tmp_name’]);

INSERTing the Image

Last but not least, you need to build the INSERT query and run it:

INSERTing the Image

Posted on January 12, 2016 in Binary Objects and Image Loading

Share the Story

Back to Top
Share This