: Undefined index: file in index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
doug76
Forum Commoner
Posts: 26
Joined: Tue Aug 24, 2010 7:44 am

: Undefined index: file in index

Post by doug76 »

Hello,
I think there may be a simple solution to this, but I cannot find it! I have a PHP program that allows a user to edit a profile they have entered along with an image. This all works perfectly. However when the submit button is clicked on I get the following error message:

Notice: Undefined index: file in C:\Program Files....editprofile1.php on line 105

Everything else has worked. The information has been uploaded to the database and all other clickable objects work. I do not understand why I get this message.

Any help would be gratefully appreciated. I am working on local host.

This is the code I have:

Code: Select all

<?php
  
session_start();

  
// If the session vars aren't set, try to set them with a cookie
  
if (!isset($_SESSION['user_id'])) {
    
if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
      
$_SESSION['user_id'] = $_COOKIE['user_id'];
      
$_SESSION['username'] = $_COOKIE['username'];
    
}
  }

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  
<title>Mismatch - Edit Profile</title>
  
<link rel="stylesheet" type="text/css" href="style.css" />

</head>

<body>
  
<h3>Mismatch - Edit Profile</h3>


<?php
  
require_once('appvars1.php');
  require_once('connectvars1.php');

  
// Make sure the user is logged in before going any further.
  
if (!isset($_SESSION['user_id'])) {
    echo '<p class="login">Please <a href="login4.php">log in</a> to access this page.</p>';
    
exit();
  
}
  else {
    echo('<p class="login">You are logged in as ' . $_SESSION['username'] . '. <a href="logout2.php">Log out</a>.</p>');

  }


  // Connect to the database
  
$dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);

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

// Grab the profile data from the POST
    
$first_name = mysqli_real_escape_string($dbc, trim($_POST['firstname']));
    
$last_name = mysqli_real_escape_string($dbc, trim($_POST['lastname']));
    
$gender = mysqli_real_escape_string($dbc, trim($_POST['gender']));
    
$birthdate = mysqli_real_escape_string($dbc, trim($_POST['birthdate']));
    
$city = mysqli_real_escape_string($dbc, trim($_POST['city']));
    
$state = mysqli_real_escape_string($dbc, trim($_POST['state']));
    
$old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture']));
    
$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($first_name) && !empty($last_name) && !empty($gender) && !empty($birthdate) && !empty($city) && !empty($state)) {
        // Only set the picture column if there is a new picture
        if (!empty($new_picture)) {
          $query = "UPDATE mismatch_user SET first_name = '$first_name', last_name = '$last_name', gender = '$gender', " .
            " birthdate = '$birthdate', city = '$city', state = '$state', picture = '$new_picture' WHERE user_id = '" . $_SESSION['user_id'] . "'";
        }


        else {
          
$query = "UPDATE mismatch_user SET first_name = '$first_name', last_name = '$last_name', gender = '$gender', " .
            " birthdate = '$birthdate', city = '$city', state = '$state' WHERE user_id = '" . $_SESSION['user_id'] . "'";
        }
        mysqli_query($dbc, $query);

        

// Confirm success with the user
        
echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile1.php">view your profile</a>?</p>';

        
mysqli_close($dbc);
        exit();
    
  }
      
else {
        echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
   
   }
    }
  } 

// End of check for form submission
  else {
    

// Grab the profile data from the database
    
$query = "SELECT first_name, last_name, gender, birthdate, city, state, picture FROM mismatch_user WHERE user_id = '" . $_SESSION['user_id'] . "'";
    
$data = mysqli_query($dbc, $query);
    
$row = mysqli_fetch_array($data);

    
if ($row != NULL) {
      $first_name = $row['first_name'];
      
$last_name = $row['last_name'];
      $gender = $row['gender'];
      
$birthdate = $row['birthdate'];
      $city = $row['city'];
      
$state = $row['state'];

      $old_picture = $row['picture'];
    
}
    else {
      echo '<p class="error">There was a problem accessing your profile.</p>';

    }
  }


  mysqli_close($dbc);

?>

  
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
    
<fieldset>
      <legend>Personal Information</legend>
      <label for="firstname">First name:</label>
      <input type="text" id="firstname" name="firstname" value="<?php if (!empty($first_name)) echo $first_name; ?>" /><br />

      <label for="lastname">Last name:</label>
      <input type="text" id="lastname" name="lastname" value="<?php if (!empty($last_name)) echo $last_name; ?>" /><br />
      <label for="gender">Gender:</label> <select id="gender" name="gender">
        <option value="M" <?php if (!empty($gender) && $gender == 'M') echo 'selected = "selected"'; 
?>>Male</option>
        <option value="F" <?php if (!empty($gender) && $gender == 'F') echo 'selected = "selected"'; ?>>Female</option>
      </select><br />


      <label for="birthdate">Birthdate:</label>
      <input type="text" id="birthdate" name="birthdate" value="<?php if (!empty($birthdate)) echo $birthdate; else echo 'YYYY-MM-DD'; ?>" /><br />


      <label for="city">City:</label>
      <input type="text" id="city" name="city" value="<?php if (!empty($city)) echo $city; ?>" /><br />
      
<label for="state">State:</label>
      

<input type="text" id="state" name="state" value="<?php if (!empty($state)) echo $state; ?>" /><br />
      

<input type="hidden" name="old_picture" value="<?php if (!empty($old_picture)) echo $old_picture; ?>" />
      <label for="new_picture">Picture:</label>
      <input type="file" id="new_picture" name="new_picture" />
      <?php if (!empty($old_picture)) {
        echo '<img class="profile" src="' . MM_UPLOADPATH . $old_picture . '" alt="Profile Picture" />';
      } 
?>
    
</fieldset>
    
<input type="submit" value="Save Profile" name="submit" />
  
</form>

</body> 

</html>
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: : Undefined index: file in index

Post by AbraCadaver »

Shouldn't this:

Code: Select all

if ($_FILES['file']['error'] == 0) {
Be this:

Code: Select all

if ($_FILES['new_picture']['error'] == 0) {
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
doug76
Forum Commoner
Posts: 26
Joined: Tue Aug 24, 2010 7:44 am

Re: : Undefined index: file in index

Post by doug76 »

Thank you very much! It should be.

If I could ask one more question. I would like the uplaod of the image to be optional. At the moment it is not. The user gets:

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in C:\Program Files... on line 93

if they do not include an image.

Do I need to have a dafault answer or is it it more simple than that.

Thanks in advance for any help
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: : Undefined index: file in index

Post by AbraCadaver »

doug76 wrote:Thank you very much! It should be.

If I could ask one more question. I would like the uplaod of the image to be optional. At the moment it is not. The user gets:

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in C:\Program Files... on line 93

if they do not include an image.

Do I need to have a dafault answer or is it it more simple than that.

Thanks in advance for any help
Move that line into:

Code: Select all

if (!empty($new_picture)) {
You need all of the code that works with the $new_image to be inside the if that checks if it's empty.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: : Undefined index: file in index

Post by pickle »

Don't double post. Locked.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Locked