Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up
<?php

$name = $_FILES["file"]["name"];
//$size = $_FILES['file']['size']
//$type = $_FILES['file']['type']


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

$error = $_FILES['file']['error'];

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

    $location = 'uploads/';

    if  (move_uploaded_file($tmp_name, $location.$name)){
        echo 'Uploaded';    
        }

        } else {
          echo 'please choose a file';
          }
    }
?>

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

i get an 'Notice: Undefined index' error message. The enctype is included in the form tag so i can't figure out what it is.. can anyone help me out??

share|improve this question
    
Is your entire code inside one file, or are they split into two seperate files? – Fred -ii- Jan 21 '14 at 23:20
1  
@Fred -ii- in single , 200% – voodoo417 Jan 21 '14 at 23:22
    
I agree. I was about to post an answer to that affect, but it's already been taken care of below. @voodoo417 – Fred -ii- Jan 21 '14 at 23:24
    
Actually, I changed my mind. I posted a working copy below. @voodoo417 – Fred -ii- Jan 21 '14 at 23:26

The first assignment throws an warning, if nothing is uploaded and the isset test is a little bit useless..

You can change your code as follows

<?php

if (isset($_FILES["file"]["name"])) {

    $name = $_FILES["file"]["name"];
    $tmp_name = $_FILES['file']['tmp_name'];
    $error = $_FILES['file']['error'];

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

        if  (move_uploaded_file($tmp_name, $location.$name)){
            echo 'Uploaded';
        }

    } else {
        echo 'please choose a file';
    }
}
?>

<form action="test.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" value="Submit">
</form>
share|improve this answer
    
thanks! the undefined index error is gone but now i get this error: – michAmir Jan 21 '14 at 23:43
    
Warning: move_uploaded_file(uploads/testhoes.jpg): failed to open stream: No such file or directory in /var/www/upload.php on line 14. Warning: move_uploaded_file(): Unable to move '/tmp/phpnvksz8' to 'uploads/testhoes.jpg' in /var/www/upload.php on line 14 – michAmir Jan 21 '14 at 23:43
    
sorry the errors above are 2 errors but i dont know how to put space between them – michAmir Jan 21 '14 at 23:46
1  
The upload directory must exist and have write permissions (i.e. 755) – Philipp Jan 22 '14 at 11:17
<form action="test.php" method="POST" enctype="multipart/form-data"> /* mistake here: change test.php to your source: upload.php */
    <input type="file" name="file"><br><br>
    <input type="submit" value="Submit">
</form>
share|improve this answer

If you're using your entire code as one file (which I suspect you are), then you need to do the following using a conditional statement, which I tested (and working) before posting.

Plus, make sure that your uploads folder has proper write permissions set and it exists.

<?php

if(isset($_POST['submit'])){
$name = $_FILES["file"]["name"];
//$size = $_FILES['file']['size']
//$type = $_FILES['file']['type']

$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];

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

    $location = 'uploads/';

    if  (move_uploaded_file($tmp_name, $location.$name)){
        echo 'Uploaded';    
        }

        } else {
          echo 'please choose a file';
          }
    }
}
?>

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

Footnotes:

I added a conditional statement:

if(isset($_POST['submit']))

and I named the submit button: (to work in conjunction with the isset() conditional statement)

<input type="submit" name="submit" value="Submit">

N.B.: If you are in fact using your posted code as two seperate files, then you can simply copy the PHP in this answer, along with naming your present submit button set in a seperate HTML form as name="submit" (calling your form upload_form.htm for example) as I did shown above, yet retaining the action="upload.php" and naming the PHP upload handler file accordingly.

share|improve this answer
    
just like the answer from phillip the undefined index error is gone. Thanks! – michAmir Jan 21 '14 at 23:44
    
You're welcome, glad I could help. @user3221348 – Fred -ii- Jan 21 '14 at 23:45
    
but now i get this error: Warning: move_uploaded_file(uploads/testhoes.jpg): failed to open stream: No such file or directory in /var/www/upload.php on line 14. Warning: move_uploaded_file(): Unable to move '/tmp/phpnvksz8' to 'uploads/testhoes.jpg' in /var/www/upload.php on line 14 – michAmir Jan 21 '14 at 23:45
    
Does the folder exist and is it writeable? @user3221348 meaning, does it have the proper write permissions? – Fred -ii- Jan 21 '14 at 23:46
    
sorry these are 2 errors above eachother.. – michAmir Jan 21 '14 at 23:46

Resolved Undefined Index in php while uploading file
due to maximum file size limit
changes in php.ini

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

change according to your requirements

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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