<?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...?

Dani AI

Generated

Short diagnosis: the notice appears because the script reads $_FILES['file'] before a file upload exists. In the original post the variables are populated from $_FILES on every page load, so on the first (GET) visit PHP raises an "Undefined index" notice. was right to suggest checking for the existence of the upload before accessing its elements — that prevents the notice and lets you distinguish "no submission" from other upload failures.

Quick checklist and practical checks to make the upload robust:

  • Confirm you only access $_FILES['file'] after detecting a POST/upload (check request method or the existence of the $_FILES key).
  • Always inspect $_FILES['file']['error'] and handle the cases. Common constants:
    • UPLOAD_ERR_OK (0) — success
    • UPLOAD_ERR_INI_SIZE (1), UPLOAD_ERR_FORM_SIZE (2) — size limits exceeded
    • UPLOAD_ERR_PARTIAL (3) — partial upload
    • UPLOAD_ERR_NO_FILE (4) — no file uploaded
    • UPLOAD_ERR_NO_TMP_DIR (6), UPLOAD_ERR_CANT_WRITE (7), UPLOAD_ERR_EXTENSION (8) — server-side problems
  • Check server settings if you see INI-related errors: file_uploads must be On, upload_max_filesize and post_max_size must be large enough (and post_max_sizeupload_max_filesize), and the PHP temp directory must exist.

Additional practical tips and cautions: verify the destination directory exists and that the webserver account has write permission (on Windows check the IIS/Apache user). Use is_uploaded_file() before moving and check the return value of move_uploaded_file() so you can report a clear error. Sanitize or replace original filenames, use unique names, validate file type (finfo) and keep uploads outside the web root where possible. During debugging a var_dump($_FILES) on a POST will show exactly what PHP received; turn off verbose error display in production.

Finally, the notice itself can be hidden by changing error_reporting, but the right fix is the logic check (as suggested). This both removes the notice and makes the upload path handle real errors correctly.

Recommended Answers

All 2 Replies

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.