SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question 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!
    PHP Code:
    // 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/10241).'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'] > && $_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;
          break;
          }
        }
      
      if (
    $sizeOK && $typeOK) {
        
    //switch($_FILES['image']['error']) {
    //      case 0:
    //        include('create_both.inc.php');
    //        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:
    HTML Code:
    <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">
    <?php
    do {  
    ?><option value="<?php echo $row_getTheme['theme_id']?>"><?php echo $row_getTheme['theme']?></option>
    <?php
    } 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);
      }
    ?>
    </select>
    </td>
    <tr>
    <tr valign="baseline">
    <td nowrap align="right" valign="top"><label for="image">Upload image:</label></td>
    <td>
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
    <input type="file" name="image" id="image" /> 
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Caption:</td>
    <td><input type="text" name="caption" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td><input name="upload" type="submit" id="upload" value="Upload"></td>
    </tr>
    </table>
    <input type="hidden" name="MM_insert" value="uploadImage">
    </form>
    Anyone see where the error is coming from?
    TYIA!
    toad78

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://us3.php.net/manual/en/feature...ost-method.php

    You are missing the enctype attribute on your form

  3. #3
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Doh!

  4. #4
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question

    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:
    GetSQLValueString($_POST['image'], "text"),


    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:
    GetSQLValueString($file, "text"),
    ?

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Change it to
    PHP Code:
    GetSQLValueString($_FILES['image']['name'], "text"), 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Talking

    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?

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should read the page Dan linked


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •