Jump to content

Undefined Index


anevins

Recommended Posts

Problem:

Undefined Index on all 'new_picture'.

 

Code of problem:

$new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
    $new_picture_type = $_FILES['new_picture']['type'];
    $new_picture_size = $_FILES['new_picture']['size']; 
    list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);

 

Entire code:

<?php
  require_once('appvars.php');
  include_once('connectvars.php');

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ("Cant connect to server " . mysql_error());
if (mysqli_connect_errno()) {
    echo "Connect failed: " . mysqli_connect_error();
    exit();
}

if(isset($_POST['upload'])){

$new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
    $new_picture_type = $_FILES['new_picture']['type'];
    $new_picture_size = $_FILES['new_picture']['size']; 
    list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);
    $error = false;

    // Validate and move the uploaded picture file, if necessary
    if (!empty($new_picture)) {
      if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||
        ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= MM_MAXFILESIZE) &&
        ($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) {
        if ($_FILES['file']['error'] == 0) {
          // Move the file to the target upload folder
          $target = MM_UPLOADPATH . basename($new_picture);
          if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) {
            // The new picture file move was successful, now make sure any old picture is deleted
            if (!empty($old_picture) && ($old_picture != $new_picture)) {
              @unlink(MM_UPLOADPATH . $old_picture);
            }
          }
          else {
            // The new picture file move failed, so delete the temporary file and set the error flag
            @unlink($_FILES['new_picture']['tmp_name']);
            $error = true;
            echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
          }
        }
      }
      else {
        // The new picture file is not valid, so delete the temporary file and set the error flag
        @unlink($_FILES['new_picture']['tmp_name']);
        $error = true;
        echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) .
          ' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.</p>';
      }
    }

    // Update the profile data in the database
    if (!$error) {
      if (!empty($new_picture)) {
          $query = "INSERT INTO identification (id, image_url) VALUES ('', $new_picture)";
        }
        mysqli_query($dbc, $query);

        // Confirm success with the user
        echo '<p>Image successfully uploaded</p>';
	echo '<img src=" '.$new_picture.'" alt="Uploaded Image" />';
}

}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<h2>Upload a Photo</h2>
<table>
	<tr>
		<td>
			<label for="new_picture">Upload Identification:</label>
		</td>
		<td>
			<input type="file" id="new_picture" name="new_picture" />
		</td>
	</tr>
	<tr>
		<td>
			 
		</td>
		<td>
			<input type="submit" value="Upload" name="upload" />
		</td>
	</tr>
</table>
</form>

 

I don't know how to fix this problem,

has anyone got ideas?

 

 

Link to comment
Share on other sites

im sorry about the blog post! im having an issue with wordpress there messing up the code..im going to look at fixing that tomorrow.

 

so you added enctype="multipart/form-data" ??

 

im also thinking you should check that $_FILES['new_picture']['tmp_name']  is set, just in case no file is being selected:

 

if ( isset ( $_FILES['new_picture'] ) && isset ( $_FILES['new_picture']['tmp_name'] ) ) {

 

could you also maybe past the output of the following code, put this after your check for $_POST['upload']

 

echo '<pre>'; print_r ( $_FILES ); echo '</pre>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.