20

This is my error:

Notice: Undefined index: file in C:\xampp\htdocs\Project\Template1\users\index.php on line 21 Notice: Undefined index: file in C:\xampp\htdocs\Project\Template1\users\index.php on line 23 please uploaded

How to get rid of it?

Html Code:

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

Php Code:

<?php

    $name = $_FILES['file']['name'];
    $temp_name = $_FILES['file']['temp_name'];

    if (isset($name)) {

        if (!empty($name)) {
            $location = '../uploads/';
        }

        if (move_uploaded_file($temp_name, $location.$name)) {
            echo 'uploaded';
        }

    } else {
        echo 'please uploaded';
    }
?>
0

10 Answers 10

35

Make sure you have set form attribute enctype="multipart/form-data".

This attribute help you to get files from user.

<form action="PATH" method="post" enctype="multipart/form-data"></form>
0
20

Change your PHP script as below and try

<?php 
    if(isset($_POST['submit'])){
        $name       = $_FILES['file']['name'];  
        $temp_name  = $_FILES['file']['tmp_name'];  
        if(isset($name) and !empty($name)){
            $location = '../uploads/';      
            if(move_uploaded_file($temp_name, $location.$name)){
                echo 'File uploaded successfully';
            }
        } else {
            echo 'You should select a file to upload !!';
        }
    }
?>
0
7

this happens due to the size of the file :

max_execution_time = 300
max_input_time = 240
post_max_size = 128M upload_max_filesize = 128M

in your php.ini file you should change above codes according to your requirement...

1
  • I believe this is most of the case.
    – Scott Chu
    Jun 22, 2020 at 7:55
1

Do a check around your PHP code block checking if either the submit button has been pressed or if isset($_FILES['file']). This should remove your errors. They pop up because the $_FILES['file'] isn't populated before the submit button is pressed.

1

Usually, the problem is forgetting to add this line as a form tag attribute.

enctype="multipart/form-data"

The enctype attribute specifies how the form-data should be encoded when submitting it to the server.

Note: The enctype attribute can be used only if method="post".

1

Spelling mistake:

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

    $temp_name = $_FILES['file']['tmp_name']; // tmp_name

    if(isset($name)){
        if(!empty($name)){

            $location = '../uploads/';
        }
        if(move_uploaded_file($temp_name, $location.$name)){
            echo 'uploaded';
        }
    }  else {
        echo 'please uploaded';
    }
?>
2
  • Notice: Undefined index: file in C:\xampp\htdocs\Project\Template1\users\index.php on line 21 still getting this error. **Line 21 ** $name = $_FILES['file']['name']; Sep 26, 2013 at 12:23
  • If the file has not been uploaded then $_FILES['file'] will not exist. So check isset($_FILES['file']) before you use it, or supress the warnings with @
    – EJTH
    Sep 26, 2013 at 12:26
0
$upload_dir="../uploads";
$target_file="";
$tmp_file="";
if(isset($_POST['submit']))
{

        $tmp_file=$_FILES['file']['tmp_name'];
        $target_file=basename($_FILES['file']['name']);
            if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file))
        {   
        echo "File uploaded <br />";

        }
        else {
              echo "Something went Wrong !!<br/>";
            }
}
0

if you are getting Notice: Undefined index: zip_file in error message most of the time, While uploading any file to server using php, Then here is the solution for this. only you need to mention enctype type in form tag.

<form method="post" action="" name="login" enctype="multipart/form-data">
0
-1

Check if file_uploads is enabled on your php.ini

file_uploads = On
1
  • @thanksd what you wrote is incorrect. I have a good motivation because if the index is undefined on upload may depend of $ _FILES is empty, and therefore, if $_FILES is empty might depend that file_uploads is disabled on php.ini
    – fvlgnn
    May 10, 2017 at 9:20
-3

the solid solution to this problem is to use

if(isset($_POST['submit-button'])){
 $option="";
  $option=$_POST["anbieterin_geburtstag_month"];

  echo $option;
  }

always use isset function

Not the answer you're looking for? Browse other questions tagged or ask your own question.