0

Why this php code shows me error as

Notice: Undefined index: f in E:\xampp\htdocs\tests\file handling\file_upload1.php on line 7

Please help me out

<?php
     echo '<form action="file_upload1.php" method="POST" enctype="multipart/form-data">
         <input type="file" name="f"><br><br>
         <input type="submit" value="upload">
     </form>';

     $name=$_FILES['f']['name'];
     echo $name;    
?>
Debesh Mohanty
  • 469
  • 1
  • 5
  • 18
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Mar 15 '15 at 00:24

1 Answers1

1

in this example you first check if $_FILES['f'] actually exists:

<?php
 echo '<form action="file_upload1.php" method="POST" enctype="multipart/form-data">
     <input type="file" name="f"><br><br>
     <input type="submit" value="upload">
 </form>';

 $name=($_FILES['f'])?$_FILES['f']['name']: '';
 echo $name;   

or try something like this:

$name='';
if(isset($_FILES['f']){
    $name=$_FILES['f']['name'];
}

the problem is that on the same page as you create the form you want to use a variable that hold the sended file.