anevins Posted March 16, 2011 Share Posted March 16, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/230804-undefined-index/ Share on other sites More sharing options...
glenelkins Posted March 16, 2011 Share Posted March 16, 2011 you need to set the enctype on your form Here is my basic file upload example: http://thewebsolutionprovider.com/php/file-upload-with-php/ Quote Link to comment https://forums.phpfreaks.com/topic/230804-undefined-index/#findComment-1188216 Share on other sites More sharing options...
anevins Posted March 16, 2011 Author Share Posted March 16, 2011 Thanks, I've added that bit in the form tag. I am still getting an 'undefined index' problem :-\ Quote Link to comment https://forums.phpfreaks.com/topic/230804-undefined-index/#findComment-1188217 Share on other sites More sharing options...
glenelkins Posted March 16, 2011 Share Posted March 16, 2011 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/230804-undefined-index/#findComment-1188285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.