-2

I am writing PHP code in Netbeans. Program is generating the correct output but the following notice is generating on the browser.

Notice: Undefined index: name in C:\xampp\htdocs\PhpProject3\index.php on line 2

Notice: Undefined index: name in C:\xampp\htdocs\PhpProject3\index.php on line 3

Notice: Undefined index: name in C:\xampp\htdocs\PhpProject3\index.php on line 4

Notice: Undefined index: name in C:\xampp\htdocs\PhpProject3\index.php on line 5

Here is my code:

       <?php
       echo $name=$_FILES['name']['name'].'<br>';
       echo $size=$_FILES['name']['size'].'<br>'; 
       echo $type=$_FILES['name']['type'].'<br>';
       echo $tmp_name=$_FILES['name']['tmp_name'];
       ?>



      <form action="index.php" method="POST" enctype="multipart/form-data">
      <input type="file" name="name"><br/><br/>
      <input type="submit" value="Submit">
      </form>

Screen shot of the output

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Because `$_FILES` doesn't have a `name` index? What part of the error message do you not understand? – Biffen Mar 06 '17 at 09:18
  • 1
    you must check if the form has been submitted – hassan Mar 06 '17 at 09:18
  • 4
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – hassan Mar 06 '17 at 09:27

4 Answers4

3

You need to add an IF statement because PHP wants to access to the variable $_FILES['name'] even it's not set.

Then, you need to check this value if you are with a POST request (user submits the form) with the code:

if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['btnSubmit'])
{
   // get if the user submits the file
   if(isset($_FILES['name'])
   {
      echo $name=$_FILES['name']['name'].'<br>';
      echo $size=$_FILES['name']['size'].'<br>'; 
      echo $type=$_FILES['name']['type'].'<br>';
      echo $tmp_name=$_FILES['name']['tmp_name'];
   }
}

And replace your button to:

<input type="submit" value="Submit" name="btnSubmit">
Loic P.
  • 691
  • 6
  • 18
2

Try this:

<?php
    if(isset($_POST['btnSubmit']){
        echo $name=$_FILES['name']['name'].'<br>';
        echo $size=$_FILES['name']['size'].'<br>'; 
        echo $type=$_FILES['name']['type'].'<br>';
        echo $tmp_name=$_FILES['name']['tmp_name'];
    }
?>



<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="name"><br/><br/>
    <input type="submit" value="Submit" name="btnSubmit">
</form>
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
2

You are getting that error because when the page loads, the $_POST variables does not exists, therefore you first need to check if post variables exists.

 <?php

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

    if (isset($_FILES['name'])) {

        echo $name = $_FILES['name']['name'] . '<br>';
        echo $size = $_FILES['name']['size'] . '<br>';
        echo $type = $_FILES['name']['type'] . '<br>';
        echo $tmp_name = $_FILES['name']['tmp_name'];
    }


}

?>



      <form action="index.php" method="POST" enctype="multipart/form-data">
      <input type="file" name="name"><br/><br/>
      <input type="submit" value="Submit" name="submitBtn">
      </form>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
0

Try this

<?php
if(isset($_FILES['name']) && is_array($_FILES['name']))
{
    echo $name=$_FILES['name']['name'].'<br>';
    echo $size=$_FILES['name']['size'].'<br>'; 
    echo $type=$_FILES['name']['type'].'<br>';
    echo $tmp_name=$_FILES['name']['tmp_name'];
}
?>



<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="name"><br/><br/>
<input type="submit" value="Submit">
</form>
Stevie G
  • 5,638
  • 1
  • 10
  • 16
  • Obviously in a production environment you would adjust your PHP error_reporting, but that wasn't the question. – Stevie G Mar 06 '17 at 09:54
  • are you answering my comment above? – Masivuye Cokile Mar 06 '17 at 09:55
  • Yes i was merely stating that he asked why the following error was generating, not how to suppress the error altogether. – Stevie G Mar 06 '17 at 10:02
  • so by answering with **try this** answers the why part? – Masivuye Cokile Mar 06 '17 at 10:05
  • Mate, look im not on here to argue with somebody who clearly knows everything. Im just putting my solution in. He is clearly getting the notice because the PHP being executed is being executed both before and after the HTML form has been posted. – Stevie G Mar 06 '17 at 10:11
  • 1
    I'm also not here to argue with u and I don't know everything, m here to learn, I also used to answer questions with **try this** but someone taught me that answering with **try this** does not help the OP to understand what they did wrong, that's what I was trying to tell you. posting your solution with no explanation does not help OP and future users with same problem – Masivuye Cokile Mar 06 '17 at 10:16
  • Ok, Thanks. Ive only just started helping out so appreciate it. – Stevie G Mar 06 '17 at 11:06