Link to home
Avatar of jbrich01
jbrich01Flag for United States of America

asked on

Upload images to a MySQL database (PHP) doesn't work in IE 8

So I was testing out the insert image code from :

It works great in IE 6 and IE 7, but not in IE 8.

Here is the code:

<?php define ('MAX_FILE_SIZE', 1024 * 50); ?>
<?php require_once('Connections/testConn.php'); ?>
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  return $theValue;

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadImage")) {
  // make sure it's a genuine file upload
  if (is_uploaded_file($_FILES['image']['tmp_name'])) {
	// replace any spaces in original filename with underscores
	$filename = str_replace(' ', '_', $_FILES['image']['name']);
	// get the MIME type
	$mimetype = $_FILES['image']['type'];
	if ($mimetype == 'image/pjpeg') {
		$mimetype= 'image/jpeg';
	// create an array of permitted MIME types
	$permitted = array('image/gif', 'image/jpeg', 'image/png');
	// upload if file is OK
	if (in_array($mimetype, $permitted) 
		&& $_FILES['image']['size'] > 0 
		&& $_FILES['image']['size'] <= MAX_FILE_SIZE) {
	  switch ($_FILES['image']['error']) {
		case 0:
		  // get the file contents
		  $image = file_get_contents($_FILES['image']['tmp_name']);
		  // get the width and height
		  $size = getimagesize($_FILES['image']['tmp_name']);
		  $width = $size[0];
		  $height = $size[1];

  $insertSQL = sprintf("INSERT INTO images (filename, mimetype, caption, image, width, height) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($filename, "text"),
                       GetSQLValueString($mimetype, "text"),
                       GetSQLValueString($_POST['caption'], "text"),
                       GetSQLValueString($image, "text"),
                       GetSQLValueString($width, "int"),
                       GetSQLValueString($height, "int"));

  mysql_select_db($database_testConn, $testConn);
  $Result1 = mysql_query($insertSQL, $testConn) or die(mysql_error());
		  if ($Result1) {
			$result = "$filename uploaded successfully.";
		  } else {
			$result = "Error uploading $filename. Please try again.";
		case 3:
		case 6:
		case 7:
		case 8:
		  $result = "Error uploading $filename. Please try again.";
		case 4:
		  $result = "You didn't select a file to be uploaded.";
	} else {
	  $result = "$filename is either too big or not an image.";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File upload to database</title>

// if the form has been submitted, display result
if (isset($result)) {
  echo "<p><strong>$result</strong></p>";
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="uploadImage" id="uploadImage">
		<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
        <label for="image">Upload image:</label>
        <input type="file" name="image" id="image" /> 
      <label for="caption">Caption:</label>
      <input type="text" name="caption" id="caption" />
        <input type="submit" name="upload" id="upload" value="Upload" />
    <input type="hidden" name="MM_insert" value="uploadImage" />

Open in new window

Avatar of arnold
Flag of United States of America image

how big was the file?
MAX_FILE_SIZE is defined as 50k.

You do not have a check on the size of a file.
Avatar of jbrich01


Hey Arnold,

Sorry for the delayed response, I had a migraine today.  I've tried it with a file that was 48k as well as with one that was 500k.  neiter worked.  However, I will increase the size to 1024*720 permanently.
echo out to a file the contents of $insertSQL after line 76 and see how the information between 6,7 and 8 differ.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Hey Arnold, OK, echo out that line in 6,7, and 8.

Hi Ray, what I mean is that in IE 8 it does not do anthing.  No error messages, and it does not post to the DB.  But in IE 6 it actually shows up in the DB and it the images shows on the display page.

Also, thanks for the suggestion regarding uploading the images in a file system. I started that process and had some trouble with the display page (it was showing all of the images at once instead of one at a time).  So while looking for a solution, I found this link, and I changed my strategy.

They suggested that it was best to save the images in a DB because of:

  ¦ Referential integrity
  ¦ Ease of backup
  ¦ Saving of Inodes
  ¦ Easy categorisation
  ¦ Central point of operations

But, because of all of the reasons you said, I felt it was better to do develop a file system and store the URL and info in the DB.

Thank you for the code above.  And your detailed commenting.  This will really help me to understand the process.
Thanks for the points - it;s a great question, ~Ray