0

I am trying to upload an user image on server in php, but its giving me the error like bellow:

Notice: Undefined index: images in C:\xampp\htdocs\PDF\Registration\index_registration.php on line 20

Notice: Undefined index: images in C:\xampp\htdocs\PDF\Registration\index_registration.php on line 21

here's my code:

<?php include "includes/header.php"?>
<?php include "../db.php" ?>
<?php include "../functions.php" ?>

<!-- banner -->
    <div class="center-container">
    <div class="banner-dott">
        <div class="main">
            <h1 class="w3layouts_head">Readers Registration</h1>
                <div class="w3layouts_main_grid">

                    <?php
                    if (isset($_POST['submit'])){

                        $name = escape($_POST['name']);
                        $password = escape($_POST['password']);
                        $first_name = escape($_POST['first_name']);
                        $last_name = escape($_POST['last_name']);
                        $email = escape($_POST['email']);
                        $p_image = $_FILES['images']['name'];
                        $post_image_temp = $_FILES['images']['tmp_name'];
                        $role = 'subscriber';

                        move_uploaded_file($post_image_temp, "user_picture/$p_image");

                        $query = "insert into user (name, password, first_name, last_name, email, image, role) values ('{$name}', '{$password}', '{$first_name}', '{$last_name}', '{$email}','{$p_image}', '{$role}')";
                        $execute = mysqli_query($connection, $query);

                    }
                    ?>

                    <form action="" method="post" class="w3_form_post">
                        <div class="w3_agileits_main_grid w3l_main_grid">
                            <span class="agileits_grid">
                                <label>First Name </label>
                                <input autocomplete="off" type="text" name="first_name" placeholder="First Name" required="">
                            </span>
                        </div>
                        <div class="w3_agileits_main_grid w3l_main_grid">
                            <span class="agileits_grid">
                                <label>Last Name </label>
                                <input autocomplete="off" type="text" name="last_name" placeholder="Last Name" required="">
                            </span>
                        </div>
                        <div class="w3_agileits_main_grid w3l_main_grid">
                            <span class="agileits_grid">
                                <label>Your Email </label>
                                <input autocomplete="off" type="email" name="email" placeholder="Your Email" required="">
                            </span>
                        </div>
                        <div class="w3_agileits_main_grid w3l_main_grid">
                            <span class="agileits_grid">
                                <label>User Name </label>
                                <input autocomplete="off" type="text" name="name" placeholder="User Name" required="">
                                </span>
                        </div>
                        <div class="w3_agileits_main_grid w3l_main_grid">
                            <span class="agileits_grid">
                                <label>Password </label>
                                <input autocomplete="off" class="form-control mx-sm-3" type="text" name="password" placeholder="Password" required="">
                                </span>
                        </div>
                        <br>
                        <div class="input-group mb-3">
                            <div class="custom-file">
                                <input type="file" class="custom-file-input" id="inputGroupFile01">
                                <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
                            </div>
                        </div>
                    <div class="w3_main_grid">
                        <div class="w3_main_grid_right">
                            <input type="submit" name="submit" value="Submit">
                        </div>
                    </div>
                </form>
            </div>

<?php include "includes/footer.php"?>

Note: I took a folder named user_picture to store all pictures, and using a bootstrap class as a file uploader. CAN ANYONE PLEASE HELP ME TO FIGURE OUT MY ERROR...!

java
  • 33
  • 6
  • 1
    It clearly says that you are missing `'images'` index in your `$_FILES` array. The `attribute` `name` is passed to the server side script. Your `input[file]` doesn't have a `name` attribute. Therefore it cannot be found by your serverside script (php). Also you are missing `enctyp` on your `
    `.
    – Ivanka Todorova Jul 05 '18 at 09:52

3 Answers3

1

Need to add name attribute to file input field, only then images can be retrieved from $_FILES variable in PHP file. Code for reference:

<input type="file" class="custom-file-input" id="inputGroupFile01" name="images">

Also add enctype attribute to form to allow posting media files like:

<form action="" method="post" class="w3_form_post" enctype="multipart/form-data">
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
0

Form element must look like this:

<form action="" method="post" class="w3_form_post" enctype="multipart/form-data">
Bas
  • 2,330
  • 4
  • 29
  • 68
-1

In your code i found this two issue please check,

<form action="" method="post" class="w3_form_post" enctype="multipart/form-data">
<input type="file" class="custom-file-input" name="images" id="inputGroupFile01">

I hope this will help you.