0

i check all topics in stack about files and undefined index error but cant find answer anywhere. I have two files:

wpis.html

    <form action="wpis.php" method="POST">
    <input type="file" name="file">
    <input type="submit" name="Submit" value="Submit">
    </form>

wpis.php

if($_POST["Submit"])
{
echo $name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
}

And every time i'm getting this error : Undefined index: file in... I tried it on my computer using intellij plugin and on local host using wamp server with uploadfiles turn on. And ideas what may be the problem?

1
  • With enctype="multipart/form-data" added, your code works so this can not be reproduced. Commented Dec 5, 2015 at 23:54

2 Answers 2

2

Try setting the proper enctype in your form tag:

<form action="wpis.php" method="POST" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

Comments

0

You could try ( this was geared towards multiple file uploads but should still work ) - but as @Schlaus said - the form must have the enctype="multipart/form-data" set when uploading files.

foreach( $_FILES['file']['name'] as $i => $name ) {
    if( !empty( $_FILES['file']['tmp_name'][$i] ) ) {
        $name = $_FILES['file']['name'][$i];
        $size = $_FILES['file']['size'][$i];
        $type = $_FILES['file']['type'][$i];
        $tmp  = $_FILES['file']['tmp_name'][$i];

        echo $name,$size,$type,$tmp;
    }
}

↓↓ Working example ↓↓

html
----

<!-- target is in the same directory as html running upload form -->
<form action="upload_target.php" method="post" enctype="multipart/form-data">
    <h1>Standard File Upload</h1>
    <input type="file" name="file[]">
    <input type="submit" value="Upload">
</form>



<?php
    /* upload_target.php */

    if( $_SERVER['REQUEST_METHOD']=='POST' ){

        $fieldname='file';

        define('NL',PHP_EOL);

        foreach( $_FILES[$fieldname]['name'] as $i => $name ) {
            if( !empty( $_FILES[$fieldname]['tmp_name'][$i] ) ) {

                $name = $_FILES[$fieldname]['name'][$i];
                $size = $_FILES[$fieldname]['size'][$i];
                $type = $_FILES[$fieldname]['type'][$i];
                $tmp  = $_FILES[$fieldname]['tmp_name'][$i];

                echo '<pre>',$name,NL,$size,NL,$type,NL,$tmp,'</pre>';
            }
        }   
    }
?>

output


6a00d8341d65de53ef01156f9b94a1970b-320wi.jpg
21714
image/jpeg
C:\Program Files (x86)\PHP\uploadtemp\php22AF.tmp

5 Comments

Notice: Undefined index: files in C:\Users\Piotrek\Desktop\www\ProjektPHP\wpis.php on line 69 Warning: Invalid argument supplied for foreach() in C:\Users\Piotrek\Desktop\www\ProjektPHP\wpis.php on line 69 form : <form action="wpis.php" method="POST" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" name="Submit" value="Submit"> </form>
sorry, i had files rather than file
still nothing, is this language is so dumb or what?
one last suggestion before I go to bed, as the loop suggests an array try using file[] as the name of the input element - ie: <input type='file' name='file[]' />
I changed name from file to file[] but errow with "Invalid argument supplied for foreach()" still appear, the second one with invalid index too. Any ideas what is the reason this problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.