Viewed   169 times

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>
         ini_set("display_errors", 1); 
        //end de-bugging//
        $form = "<form action='register.php' method='post'>
        <table cellspacing='5px'>
                <td><font color='red'>*</font> are required fields.</td>
                <td>First Name:</td>
                <td><input type='text' name='firstname' class='textbox'><font color='red'> *</font></td>
                <td>Last Name:</td>
                <td><input type='text' name='lastname' class='textbox'><font color='red'> *</font></td>
                <td><input type='text' name='username' class='textbox'><font color='red'> *</font></td>
                <td><input type='text' name='email' class='textbox'><font color='red'> *</font></td>
                <td><input type='password' name='password' class='textbox'><font color='red'> *</font></td>
                <td>Confirm Password:</td>
                <td><input type='password' name='repassword' class='textbox'><font color='red'> *</font></td>
                <td>Profile Picture:</td>
                <td><input name='avatar' type='file' ></td>
                <td>Profile Message:</td>
                <td><textarea name='bio' cols='35' rows='5' class='textbox'></textarea></td>
                <td><input type='submit' name='submitbtn' value='Submit' class='button'></td>
        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) {
                        $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");

                                        move_uploaded_file($tmpname, "avatars/$username.$ext");
                                        $avatar = "$username.$ext";
                                            $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";
                                echo "That email is already taken. $form";
                            echo "That username is already taken. $form";
                        echo "You did not enter a valid email. $form";
                    echo "Your passwords did not match. $form";
                echo "You did not fill in all of the required fields. $form";
                echo "$form";
            <?php require ("styles/bottom.php") ; ?>



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

Saturday, November 12, 2022

Your form needs the enctype="multipart/form-data" attribute to be able to upload files.

More information here

Sunday, September 25, 2022

You're not telling the JS how to send your POST parameters. Change your JS to:

data: { 'testscore':testscore },

This is the equivalent of "testscore=" + testcore in key=value form. It tells JS and PHP that you want the variable testscore to be mapped to the key "testscore", which you'll then pick up with $_POST['testscore']

Edit: See, "Sending Data to the Server"

Wednesday, August 31, 2022

Some providers does not allow you change certain values in running time. Instead of this, try to either change it in the real php.ini file or use an .htaccess (For Apache web servers) where you can add your configuration. You can find more information in the article about this subject: How to change configuration settings.

Based on your story, example .htaccess

<IfModule mod_php5.c>
php_value upload_max_filesize 100000000
php_value post_max_size 110000000
php_value memory_limit 120000000
php_value max_input_time 20
Wednesday, December 14, 2022

I have tried the same code and it works for me. I have made some changes for you.

<form method="POST" enctype="multipart/form-data">
    <label for="">Upload Your Cv</label><input type="file" name="file">
    <input type="submit" name="upload" value="upload">

After that you don't need to redirect the page; instead you can use this, below the </form>

if (isset($_FILES['file'])) {
        $file   = $_FILES['file'];
        // print_r($file);  just checking File properties

        // File Properties
        $file_name  = $file['name'];
        $file_tmp   = $file['tmp_name'];
        $file_size  = $file['size'];
        $file_error = $file['error'];

        // Working With File Extension
        $file_ext   = explode('.', $file_name);
        $file_fname = explode('.', $file_name);

        $file_fname = strtolower(current($file_fname));
        $file_ext   = strtolower(end($file_ext));
        $allowed    = array('txt','pdf','doc','ods');

        if (in_array($file_ext,$allowed)) {

            if ($file_error === 0) {
                if ($file_size <= 5000000) {
                        $file_name_new     =  $file_fname . uniqid('',true) . '.' . $file_ext;
                        $file_name_new    =  uniqid('',true) . '.' . $file_ext;
                        $file_destination =  'upload/' . $file_name_new;
                        // echo $file_destination;
                        if (move_uploaded_file($file_tmp, $file_destination)) {
                                echo "Cv uploaded";
                            echo "some error in uploading file".mysql_error();
                    echo "size must bne less then 5MB";

            echo "invalid file";

Note that the upload folder must be in the same directory as where you store the file.

Sunday, October 9, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :