PHP未定义索引错误$ _FILES?

时间:2012-06-26 04:37:05

标签: php file upload error-handling undefined

我是PHP新手,正在关注YouTube上的教程。我有一切在这个文件中工作,除了文件上传,任何帮助将不胜感激。这是我得到的错误:

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

  

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

     

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

     

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

     

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

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

            <?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") ; ?>

4 个答案:

答案 0 :(得分:22)

要上传文件,您必须在表格标记中使用enctype。

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

答案 1 :(得分:10)

首先:尝试严格编程

error_reporting(E_ALL | E_STRICT);

你也必须使用isset for check is index for array available not not

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

还要检查php配置

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

发布最大尺寸必须大于上传最大文件大小。

也有人说检查表 enctype

答案 2 :(得分:5)

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

答案 3 :(得分:3)

这是1年前发布但你可能仍然感兴趣.. 在PHP.ini中选中此选项:     enable_post_data_reading =关 它应该是:否则$ _FILES将永远是空的 PHP是否将读取POST数据默认情况下启用此选项。 最有可能的是,您不希望全局禁用此选项。它导致$ _POST 和$ _FILES总是为空。