<?php
     $filename = $_FILES['file']['name'];
     $tempname = $_FILES['file']['tmp_name'];

     if(isset($filename)){
          //echo $filename;
          $directory = 'E:/php_uploads/';
          move_uploaded_file($tempname,$directory.$filename);
          echo 'Done';
     }
     else{
          echo 'No file selected';
     }
?>

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

The enctype is correct.The element names correspond to those in the php code.So what is wrong which leads to it showing Notice: Undefined index : file in blah blah blah...?

Hi delta_frost,

You have to do it this way, as on the initial load of the page, it can't find any set $_FILE["file"], in the $_FILE global. Why? there where no form submissions yet.

     if(isset($_FILES['file'])){
         $filename = $_FILES['file']['name'];
         $tempname = $_FILES['file']['tmp_name'];
          //echo $filename;
          $directory = 'E:/php_uploads/';

Thanks, I was trying this by watching a tutorial by someone from thenewBoston. His code was exactly the same as mine above, but I suspect that turning error_output on to some value caused the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.