File Upload Error: Notice: Undefined index: image in

I’m trying to integrate a borrowed code to upload a file, record the theme and filename to the database. After pressing ‘upload’ button, I receive this error in several places of my code: Notice: Undefined index: image in C:\htdocs\sherryscustomcakes\admin\cake_gallery\upload_gallery_files.php on line 45

I checked my form and made sure the names coincide with the upload code, but can’t seem to figure out where I went wrong!

// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 204800);

if (array_key_exists('upload', $_POST)) {
  // define constant for upload folder
  define('UPLOAD_DIR', '../../images/gallery/');
  // replace any spaces in original filename with underscores
  // at the same time, assign to a simpler variable
  $file = str_replace(' ', '_', $_FILES['image']['name']);
  // convert the maximum size to KB
  $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  // create an array of permitted MIME types
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  // begin by assuming the file is unacceptable
  $sizeOK = false;
  $typeOK = false;

  // check that file is within the permitted size
  if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
    $sizeOK = true;

  // check that file is of an permitted MIME type
  foreach ($permitted as $type) {
    if ($type == $_FILES['image']['type']) {
      $typeOK = true;

  if ($sizeOK && $typeOK) {
    //switch($_FILES['image']['error']) {
//	  case 0:
//		include('');
//	    break;
//	  case 3:
//		$result = "Error uploading $file. Please try again.";
//	  default:
//        $result = "System error uploading $file. Contact webmaster.";
//	  }

 // elseif ($_FILES['image']['error'] == 4) {
//    $result = 'No file selected';
//	}
//  else {
//    $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
//	}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadImage")) {
  $insertSQL = sprintf("INSERT INTO gallery (theme_id, image, caption) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['theme_id'], "int"),
                       GetSQLValueString($_POST['image'], "text"),
                       GetSQLValueString($_POST['caption'], "text"));

  mysql_select_db($database_sc_admin, $sc_admin);
  $Result1 = mysql_query($insertSQL, $sc_admin) or die(mysql_error());

  $insertGoTo = "gallery_list.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  header(sprintf("Location: %s", $insertGoTo));


<form method="post" name="uploadImage" id="uploadImage" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Theme:</td>
<td><select name="theme_id">
do {
?><option value="<?php echo $row_getTheme['theme_id']?>"><?php echo $row_getTheme['theme']?></option>
} while ($row_getTheme = mysql_fetch_assoc($getTheme));
  $rows = mysql_num_rows($getTheme);
  if($rows > 0) {
      mysql_data_seek($getTheme, 0);
	  $row_getTheme = mysql_fetch_assoc($getTheme);
<tr valign="baseline">
<td nowrap align="right" valign="top"><label for="image">Upload image:</label></td>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="file" name="image" id="image" />
<tr valign="baseline">
<td nowrap align="right">Caption:</td>
<td><input type="text" name="caption" value="" size="32"></td>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input name="upload" type="submit" id="upload" value="Upload"></td>
<input type="hidden" name="MM_insert" value="uploadImage">

Anyone see where the error is coming from?

You are missing the enctype attribute on your form


OK. That fixed the majority of the Undefined index errors except I have one that says it on line 88. Line 88 looks like this:
[CENTER]GetSQLValueString($_POST[‘image’], “text”),[/CENTER]

The files upload and the ‘theme_id’ records in the database, but the filename does not. I’m guessing I need to change the above to:

[CENTER]GetSQLValueString($file, “text”),[/CENTER] ?

Change it to

GetSQLValueString($_FILES['image']['name'], "text"),


Thank you!

Now I need to figure out how to create multiple file uploads. Should I post it here or should I post a new thread?

You should read the page Dan linked :slight_smile: