6

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") ; ?>
Community
  • 1
  • 1
mais-oui
  • 2,691
  • 6
  • 19
  • 15
  • `var_dump($_FILES);` And **always** use `var_dump();` to see what's inside the variable – zerkms Jun 26 '12 at 04:39
  • On a side note, I find it kind of odd how you wrap all your data checking inside one recursive `if` statement. What happens if the username is already taken **AND** the user enters an invalid email? Wouldn't it be nice to let them know *ALL* the errors at once so they don't have to fix one at a time? Personally, I add all my errors to an array, like `$err[] = 'username empty'` then just check if $err `isset` further down and loop through it showing all the errors. – Mike Jun 26 '12 at 04:46
  • @Mike Thank you for the suggestion. Right now, I am not advanced enough, but I will try doing this in the future. – mais-oui Jun 26 '12 at 04:49

4 Answers4

29

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

<form enctype="multipart/form-data" action='register.php' method='post'>
Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
  • Then check whether the file size your are uploading is greater than the limit defined in php.ini. If you do not know how, try with a small image (few kB) – Sarwar Erfan Jun 26 '12 at 04:47
  • Stupid question, but did you refresh the page before you submitted it again to test if it worked? – Mike Jun 26 '12 at 04:48
  • No I didn't, but I've made that mistake myself before, so just making sure you weren't. – Mike Jun 28 '12 at 20:26
13

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

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

also check php configuraion

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

  • The `if (isset($_POST['submitbtn']) && isset($_FILES['avatar'])) { // ... }` works, but then I get Undefined index: tmpname in /Applications/xxx – mais-oui Jun 26 '12 at 04:46
  • where should I add `file_uploads "1" upload_max_filesize "2M" post_max_size "8M" max_file_uploads 20` ? Thank you for your help and quick replies. – mais-oui Jun 26 '12 at 04:54
  • if you use apache you can add them to with [How to change configuration settings](http://php.net/manual/en/configuration.changes.php) ... or php.ini ... after restar apache (or phpcgi) check in phpinfo() for confirm. – Mohammad Hossein Fattahizadeh Jun 26 '12 at 04:56
5

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

Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
4

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.

Marvin Mustafa
  • 163
  • 1
  • 8