$ _FILES未定义索引错误

时间:2015-12-05 23:16:20

标签: php file-upload

我检查堆栈中关于文件和未定义索引错误的所有主题,但无法在任何地方找到答案。我有两个文件:

wpis.html

lpProcessAttributes

wpis.php

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

每次我收到此错误:未定义的索引:文件在...... 我在我的计算机上使用intellij插件尝试了它,在本地主机上使用wamp服务器和uploadfiles打开。想法可能是什么问题?

2 个答案:

答案 0 :(得分:1)

尝试在表单标记中设置正确的enctype:

<form action="wpis.php" method="POST" enctype="multipart/form-data">

答案 1 :(得分:0)

您可以尝试(这适用于多个文件上传但仍应有效) - 但正如@Schlaus所说 - 表单在上传文件时必须设置enctype="multipart/form-data"

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;
    }
}

↓↓工作实例↓↓

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>';
            }
        }   
    }
?>
  

输出

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