PHP 未定义索引错误 $_FILES?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11200843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:58:00  来源:igfitidea点击:

PHP Undefined index error $_FILES?

phpfileuploaderror-handlingundefined

提问by mais-oui

I am new to PHP and am following a tutorial on YouTube. I have everything working in this file, except for the file uploading, any help would be appreciated. Here is the error i am getting:

我是 PHP 新手,正在学习 YouTube 上的教程。除了文件上传,我在这个文件中的所有内容都在工作,任何帮助将不胜感激。这是我得到的错误:

*NOTE: I have looked for this many times, but could not find undefined index error relevant to $_FILES...

*注意:我已经找了很多次了,但找不到与 $_FILES 相关的未定义索引错误...

Notice: Undefined index: avatar in /Applications/xxx on line 95

Notice: Undefined index: avatar in /Applications/xxx on line 96

Notice: Undefined index: avatar in /Applications/xxx on line 97

Notice: Undefined index: avatar in /Applications/xxx on line 98

注意:未定义索引:第 95 行 /Applications/xxx 中的头像

注意:未定义索引:第 96 行 /Applications/xxx 中的头像

注意:未定义索引:第 97 行 /Applications/xxx 中的头像

注意:未定义索引:第 98 行 /Applications/xxx 中的头像

Sorry for this if it is a simple fix...

对不起,如果这是一个简单的修复......

            <?php $title = "Register";?>
        <?php require ("styles/top.php") ; ?>           
        <?php //de-bugging remove this after script works as desired>
        error_reporting(E_ALL);
         ini_set("display_errors", 1); 
        //end de-bugging//

        $form = "<form action='register.php' method='post'>
        <table cellspacing='5px'>
            <tr>
                <td></td>
                <td><font color='red'>*</font> are required fields.</td>
            </tr>
            <tr>
                <td>First Name:</td>
                <td><input type='text' name='firstname' class='textbox'><font color='red'> *</font></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input type='text' name='lastname' class='textbox'><font color='red'> *</font></td>
            </tr>
            <tr>
                <td>Username:</td>
                <td><input type='text' name='username' class='textbox'><font color='red'> *</font></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type='text' name='email' class='textbox'><font color='red'> *</font></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='password' class='textbox'><font color='red'> *</font></td>
            </tr>
            <tr>
                <td>Confirm Password:</td>
                <td><input type='password' name='repassword' class='textbox'><font color='red'> *</font></td>
            </tr>
            <tr>
                <td>Profile Picture:</td>
                <td><input name='avatar' type='file' ></td>
            </tr>
            <tr>
                <td>Profile Message:</td>
                <td><textarea name='bio' cols='35' rows='5' class='textbox'></textarea></td>
            </tr>
            <tr>
                <td></td>
                <td><input type='submit' name='submitbtn' value='Submit' class='button'></td>

            </tr>
        </table>
        </form>";


        if ($_POST['submitbtn']) {

            $firstname = strip_tags($_POST['firstname']);
            $lastname = strip_tags($_POST['lastname']);
            $username = strip_tags($_POST['username']);
            $email = strip_tags($_POST['email']);
            $class = ($_POST['class']);
            $password = strip_tags($_POST['password']);
            $repassword = strip_tags($_POST['repassword']);
            $bio = strip_tags($_POST['bio']);
            //AVATAR UPLOAD
            $name = $_FILES['avatar'] ['name'];
            $type = $_FILES['avatar'] ['type'];
            $size = $_FILES['avatar'] ['size'];
            $tmpname = $_FILES['avatar'] ['tmpname'];
            $ext = substr($name, strrpos($name, '.'));



            if ($firstname && $lastname && $username && $email && $password && $repassword) {
                if ($password == $repassword) {
                    if (strstr($email, "@") && strstr($email, ".") && strlen($email) >= 6) {

                        require("scripts/connect.php");

                        $query = mysql_query("SELECT * FROM users WHERE username ='$username'");
                        $numrows = mysql_num_rows($query);
                        if ($numrows == 0) {

                            $query = mysql_query("SELECT * FROM users WHERE email ='$email'");
                            $numrows = mysql_num_rows($query);
                            if ($numrows == 0) {

                                $pass = (md5(md5($password)));
                                $date = date("F j, Y");

                                if($name){
                                        move_uploaded_file($tmpname, "avatars/$username.$ext");
                                        $avatar = "$username.$ext";
                                        }
                                        else
                                            $avatar = "default_avatar.png";

                                    $code = substr(md5(rand(111111111111, 99999999999999999)), 2, 25);  


                                mysql_query("INSERT INTO users VALUES ('', '$firstname', '$lastname', '$username', '$email', '$pass', '$avatar', '$bio', '', '', '$code', '', '$date')"); 

                                    $webmaster = "xxxx";
                                    $subject = "xxxx";
                                    $headers = "From:xxx<$webmaster>";
                                    $message = "xxx";

                                    mail($email, $subject, $message, $headers);

                                    echo "xxx";

                            }
                            else
                                echo "That email is already taken. $form";
                        }
                        else
                            echo "That username is already taken. $form";

                    }
                    else
                        echo "You did not enter a valid email. $form";

                }
                else
                    echo "Your passwords did not match. $form";
            }
            else
                echo "You did not fill in all of the required fields. $form";

            }
            else
                echo "$form";

            ?>

            </div>
            <?php require ("styles/bottom.php") ; ?>

回答by Sarwar Erfan

For uploading files, you must use the enctype in form tag.

对于上传文件,您必须在 form 标签中使用 enctype。

<form enctype="multipart/form-data" action='register.php' method='post'>

回答by sweb

first: try to strict programming

第一:尽量严格编程

error_reporting(E_ALL | E_STRICT);

also you must use isset for check is index for array available or not

您还必须使用 isset 来检查数组的索引是否可用

if (isset($_POST['submitbtn']) && isset($_FILES['avatar'])) {
     // ...
}

also check php configuraion

还要检查php配置

file_uploads    "1"
upload_max_filesize     "2M"
post_max_size   "8M"
max_file_uploads    20

post max size must be larger than upload max file size.

帖子最大大小必须大于上传最大文件大小。

also as guys said check form enctype

也正如人们所说的检查表单enctype

回答by Miqdad Ali

You should add form attribute enctype="multipart/form-data"to upload files

您应该添加表单属性enctype="multipart/form-data"来上传文件

回答by Marvin Mustafa

this is a 1 year ago Post but you may still interested .. Check this option in PHP.ini : enable_post_data_reading=off It should be : on otherwise the $_FILES will be empty forever Whether PHP will read the POST data This option is enabled by default. Most likely, you won't want to disable this option globally. It causes $_POST and $_FILES to always be empty.

这是 1 年前的帖子,但您可能仍然感兴趣 .. 在 PHP.ini 中检查此选项: enable_post_data_reading=off 它应该是: on 否则 $_FILES 将永远为空 PHP 是否会读取 POST 数据 此选项由启用默认。很可能,您不想全局禁用此选项。它导致 $_POST 和 $_FILES 始终为空。