【问题标题】:Why cant i upload image on Database in php? [duplicate]为什么我不能在 php 中将图像上传到数据库中? [复制]
【发布时间】:2018-07-05 09:06:51
【问题描述】:

我正在尝试用 php 在服务器上上传用户图像,但它给了我如下错误:

注意:未定义索引:第 20 行 C:\xampp\htdocs\PDF\Registration\index_registration.php 中的图像

注意:未定义索引:第 21 行 C:\xampp\htdocs\PDF\Registration\index_registration.php 中的图像

这是我的代码:

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

注意: 我使用了一个名为 user_picture 的文件夹来存储所有图片,并使用引导类作为文件上传器。谁能帮我找出我的错误...!

【问题讨论】:

  • 它清楚地表明您在$_FILES 数组中缺少'images' 索引。 attribute name 被传递到服务器端脚本。您的 input[file] 没有 name 属性。因此,您的服务器端脚本 (php) 无法找到它。您的&lt;form&gt; 上也缺少enctyp

标签: javascript php web web-deployment


【解决方案1】:

需要将name 属性添加到文件输入字段,这样才能从PHP 文件中的$_FILES 变量中检索图像。参考代码:

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

还可以在表单中添加enctype 属性以允许发布媒体文件,例如:

<form action="" method="post" class="w3_form_post" enctype="multipart/form-data">

【讨论】:

    【解决方案2】:

    表单元素必须如下所示:

    <form action="" method="post" class="w3_form_post" enctype="multipart/form-data">
    

    【讨论】:

      【解决方案3】:

      在你的代码中我发现了这两个问题,请检查,

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

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 1970-01-01
        • 2016-11-20
        • 1970-01-01
        相关资源
        最近更新 更多