Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

file - PHP Undefined index error $_FILES?

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:

*NOTE: I have looked for this many times, but could not find undefined index error relevant to $_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

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") ; ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...