Notice: Undefined index: image - unable to find the error

14,996

Solution 1

If a file was not uploaded the $_FILES array will be empty. Specifically, if the file image was not uploaded, $_FILES['image'] will not be set.

So

$file = $_FILES['image']['tmp_name'];   //Error comes from here(here is the prob!)

should be:

if(empty($_FILES) || !isset($_FILES['image']))

update

You will also have issues because you're missing the enctype attribute on your form:

<form class="form-horizontal" action="Ressave.php" method="POST" autocomplete="on" enctype="multipart/form-data">

Solution 2

In order to be able to process files in your form you need to add the enctype attribute.

<form method='POST' enctype='multipart/form-data' >
Share:
14,996
Admin
Author by

Admin

Updated on August 02, 2022

Comments

  • Admin
    Admin over 1 year

    here is my error.i dont know why it is not working.i checked all the parts but i was unable to find the error.

    Notice: Undefined index: image in C:\xampp\htdocs\Final\places\Ressave.php on line 27

    here is my html code that pass the name of input:

    <form class="form-horizontal" action="Ressave.php" method="POST" autocomplete="on">
    <div class="well">
       <legend>Photos</legend>
          <div class="control-group">
             <label class="control-label">Upload Photo: </label>
                 <div class="controls">
                       <input name="image" type="file" />
                 </div>   
         </div>
    </div>
    
                <div class="form-actions">
    
                  <button type="submit" class="btn btn-primary">Submit</button>
    
                  <button type="button" class="btn">Cancel</button>
    
                </div>
    
            </form>
    

    Ressave.php is here and can not recieve the name here,so the error occure.....

    <?php
    {       //  Secure Connection Script
        include('../Secure/dbConfig.php'); 
        $dbSuccess = false;
        $dbConnected = mysql_connect($db['hostname'],$db['username'],$db['password']);
    
        if ($dbConnected) {     
            $dbSelected = mysql_select_db($db['database'],$dbConnected);
            if ($dbSelected) {
                $dbSuccess = true;
            } else {
                echo "DB Selection FAILed";
            }
        } else {
                echo "MySQL Connection FAILed";
        }
        //  END Secure Connection Script
    }
    if(! $dbConnected )
    {
      die('Could not connect: ' . mysql_error());
    }
    
    
    {               // File Properties
    
    $file = $_FILES['image']['tmp_name'];   //Error comes from here(here is the prob!)
    
    
    if(!isset($file))
    
        echo "Please Choose an Image.";
    
    else  {
            $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    
            $image_size = getimagesize($_FILES['image']['tmp_name']);   
    
            if($image_size == FALSE)
                echo "That is not an image.";
            else 
            {
                    $lastid = mysql_insert_id();
                    echo "Image Uploaded.";             
            }
    }
    
    }
    
    {           //join the post values into comma separated 
    
        $features = mysql_real_escape_string(implode(',', $_POST['features']));
        $parking = mysql_real_escape_string(implode(',', $_POST['parking']));   
        $noise = mysql_real_escape_string(implode(',', $_POST['noise']));       
        $good_for = mysql_real_escape_string(implode(',', $_POST['good_for']));
        $ambience = mysql_real_escape_string(implode(',', $_POST['ambience']));
        $alcohol = mysql_real_escape_string(implode(',', $_POST['alcohol']));
    
    
    }
    
    
    $sql =  "INSERT INTO prestaurant ( ResName, Rating, Food_serve, Features, Parking, noise, Good_For, Ambience, Alcohol, Addition_info, Name, Address1, Zipcode1, Address2, Zipcode2, Address3, Zipcode3, City, Mobile, phone1, phone2, phone3, phone4, Email1, Email2, Fax, Website, image)". 
            "VALUES ('$_POST[restaurant_name]','$_POST[star_rating]','$_POST[food_served]','$features','$parking','$noise','$good_for','$ambience','$alcohol','$_POST[description]','$_POST[name]','$_POST[address1]','$_POST[zipcode1]','$_POST[address2]','$_POST[zipcode2]','$_POST[address3]','$_POST[zipcode3]','$_POST[city]','$_POST[mobile]','$_POST[phone1]','$_POST[phone2]','$_POST[phone3]','$_POST[phone4]','$_POST[email1]','$_POST[email2]','$_POST[fax]','$_POST[url]','$image')";
    
    
    mysql_select_db('place');
    $retval = mysql_query( $sql );
    if(! $retval )
    {    
      die('Could not enter data: ' . mysql_error());
    }
    echo "Entered data successfully\n";
    mysql_close($dbConnected);
    ?>
    
  • Admin
    Admin over 10 years
    the error is Array ( ) Please Choose an Image. Notice: Undefined variable: image in C:\xampp\htdocs\Final\places\Ressave.php on line 64
  • Admin
    Admin over 10 years
    my same code will work other place but it is not working in this part of my code
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    @EniGma Whether it makes a difference or not, it must be in your form.
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    @JohnConde I'm starting to wonder whether the OP wants a simple reference to an uploaded file, or if the OP wants the file uploaded to an actual folder. (using move_uploaded_file) which is not in the OP's code.
  • Admin
    Admin over 10 years
    when i use print_r($_POST['image']); it will show the name of the image like(winter.jpg)
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    @EniGma Look at my comment also, there's something about your if condition stackoverflow.com/questions/18747542/…
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    @EniGma What (exactly) works? What happened for it to suddenly work?
  • Admin
    Admin over 10 years
    i use john's code (if(empty($_FILES) || !isset($_FILES['image']))) and i it worked...
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    @EniGma That's great, glad to hear it. Cheers
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    ^--- that's for you @JohnConde (am sure) ;-)
  • Admin
    Admin over 10 years
    that thanx was for you either ...:)