无法使用php上传图片到mysql [已关闭]

busg9geu  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(121)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
20小时前就关门了。
Improve this question
当我试图上传多张图片到数据库时,我遇到了一个使用错误。它显示一个错误,说文件类型不允许,我检查了文件类型,它是png
仅当尝试验证文件扩展名时才会出现此问题。
我是PHP新手,请帮我修正一下。我不知道我做错了什么。有人能告诉我吗?
这是更新后的文件。
add_blog.php

<?php include 'header.php';
if (isset($_SESSION['user_data'])) {
    // This will fetch the author id that is stored in index['0'].
    $author_id = $_SESSION['user_data']['0'];

    $sql = "SELECT * FROM categories";
    $query = mysqli_query($config, $sql);
}
?>

<div class="container">
    <h5 class="mb-2 text-gray-800">School</h5>
    <div class="row">
        <div class="col-xl-8 col-lg-6">
            <div class="card">
                <div class="card-header">
                    <h6 class="font-weight-bold text-primary mt-2">Add School</h6>
                </div>
                <div class="card-body">
                    <form action="" method="POST" enctype="multipart/form-data">
                        <div class="mb-3">
                            <input type="text" name="blog_title" placeholder="Title" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_tagline" placeholder="Tagline" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_rating" placeholder="Rating" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_projects" placeholder="Projects" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_events" placeholder="School Events" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="number" name="school_mobile" placeholder="School Number" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_email" placeholder="School Email" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facility" placeholder="Facilities your school have" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_2.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facility_2" placeholder="Facilities 2" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_3.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facility_3" placeholder="Facilities 3" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_4.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facility_4" placeholder="Facilities 4" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_5.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facility_5" placeholder="Facilities 5" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_6.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facility_6" placeholder="Facilities 6" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_7.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facility_7" placeholder="Facilities 7" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_8.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facility_8" placeholder="Facilities 8" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_activities.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_activities_heading_1" placeholder="School Activities Heading" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_activities_para_1" placeholder="School activities Paragraph" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_activities_2.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_activities_heading_2" placeholder="School activities Heading 2" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_activities_para_2" placeholder="School Activities Paragraph 2" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_activities_3.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_activities_heading_3" placeholder="School activities Heading 3" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_activities_para_3" placeholder="School activities Paragraph 3" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_mission.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_mission_heading" placeholder="School Mission Heading" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_mission_para" placeholder="School Mission Paragraph" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_vision.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_vision_heading" placeholder="School Vision Heading" class="form-control">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_vision_para" placeholder="School Vision Paragraph" class="form-control">
                        </div>
                        <div class="mb-3">
                            <label for="">About School Infomartion</label>
                            <textarea class="form-control" name="blog_body" id="blog" rows="2"></textarea>
                        </div>
                        <div class="mb-3">
                            <label for="">School Activities Infomartion</label>
                            <textarea class="form-control" name="school_activities" id="blog_1" rows="2"></textarea>
                        </div>
                        <div class="mb-3">
                            <label for="">If you have mission or vision please describe</label>
                            <textarea class="form-control" name="school_mission_vision" id="blog_2" rows="2"></textarea>
                        </div>
                        <div class="mb-3">
                            <input type="file" name="blog_image" class="form-control">
                        </div>
                        <label for="">filename1</label>
                        <div class="mb-3">
                            <input type="file" name="school_image_1" class="form-control">
                        </div>
                        <label for="">filename2</label>
                        <div class="mb-3">
                            <input type="file" name="school_image_2" class="form-control">
                        </div>
                        <label for="">filename3</label>
                        <div class="mb-3">
                            <input type="file" name="school_image_3" class="form-control">
                        </div>
                        <label for="">filename4</label>
                        <div class="mb-3">
                            <input type="file" name="school_image_4" class="form-control">
                        </div>
                        <label for="">filename5</label>
                        <div class="mb-3">
                            <input type="file" name="school_image_5" class="form-control">
                        </div>
                        <label for="">filename6</label>
                        <div class="mb-3">
                            <input type="file" name="school_image_6" class="form-control">
                        </div>
                        <label for="">filename7</label>
                        <div class="mb-3">
                            <input type="file" name="school_image_7" class="form-control">
                        </div>
                        <label for="">filename8</label>
                        <div class="mb-3">
                            <input type="file" name="school_image_8" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_social.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_instagram" placeholder="Instagram Link" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_social_2.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_facebook" placeholder="Facebook Link" class="form-control">
                        </div>
                        <div class="mb-3">
                            <?php include 'icons_social_3.php'; ?>
                        </div>
                        <div class="mb-3">
                            <input type="text" name="school_twitter" placeholder="Twitter Link" class="form-control">
                        </div>
                        <div class="mb-3">
                            <select class="form-control" name="category">
                                <option value="" name="category">Select Category</option>
                                <!-- $cats will fetch all all the data that is store in the categories. -->
                                <?php while ($cats = mysqli_fetch_assoc($query)) { ?>
                                    <option value="<?= $cats['cat_id'] ?>">
                                        <?= $cats['cat_name'] ?>
                                    </option>

                                <?php } ?>
                                }
                            </select>
                        </div>
                        <div class="mb-3">
                            <input type="submit" name="add_blog" value="Add" class="btn btn-primary">
                            <a class="btn btn-secondary" href="index.php">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
include 'footer.php';

// If the user click on Add button then the user data should upload to the database.
if (isset($_POST['add_blog'])) {
    // This will make the connect to database and will post the data to the database.
    $title = mysqli_real_escape_string($config, $_POST['blog_title']);
    // This will make the connect to database and will post the data to the database.
    $tagline = mysqli_real_escape_string($config, $_POST['school_tagline']);
    // This will make the connect to database and will post the data to the database.
    $rating = mysqli_real_escape_string($config, $_POST['school_rating']);
    // This will make the connect to database and will post the data to the database.
    $projects = mysqli_real_escape_string($config, $_POST['school_projects']);
    // This will make the connect to database and will post the data to the database.
    $events = mysqli_real_escape_string($config, $_POST['school_events']);
    // This will make the connect to database and will post the data to the database.
    $mobile = mysqli_real_escape_string($config, $_POST['school_mobile']);
    // This will make the connect to database and will post the data to the database.
    $email = mysqli_real_escape_string($config, $_POST['school_email']);
    // This will make the connect to database and will post the data to the database.
    $activities = mysqli_real_escape_string($config, $_POST['school_activities']);
    // This will make the connect to database and will post the data to the database.
    $missionVision = mysqli_real_escape_string($config, $_POST['school_mission_vision']);
    // This will make the connect to database and will post the data to the database.
    $body = mysqli_real_escape_string($config, $_POST['blog_body']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon = mysqli_real_escape_string($config, $_POST['icon_input']);
    // This will make the connect to database and will post the data to the database.
    $facility_1 = mysqli_real_escape_string($config, $_POST['school_facility']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon2 = mysqli_real_escape_string($config, $_POST['icon_input_2']);
    // This will make the connect to database and will post the data to the database.
    $facility_2 = mysqli_real_escape_string($config, $_POST['school_facility_2']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon3 = mysqli_real_escape_string($config, $_POST['icon_input_3']);
    // This will make the connect to database and will post the data to the database.
    $facility_3 = mysqli_real_escape_string($config, $_POST['school_facility_3']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon4 = mysqli_real_escape_string($config, $_POST['icon_input_4']);
    // This will make the connect to database and will post the data to the database.
    $facility_4 = mysqli_real_escape_string($config, $_POST['school_facility_4']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon5 = mysqli_real_escape_string($config, $_POST['icon_input_5']);
    // This will make the connect to database and will post the data to the database.
    $facility_5 = mysqli_real_escape_string($config, $_POST['school_facility_5']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon6 = mysqli_real_escape_string($config, $_POST['icon_input_6']);
    // This will make the connect to database and will post the data to the database.
    $facility_6 = mysqli_real_escape_string($config, $_POST['school_facility_6']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon7 = mysqli_real_escape_string($config, $_POST['icon_input_7']);
    // This will make the connect to database and will post the data to the database.
    $facility_7 = mysqli_real_escape_string($config, $_POST['school_facility_7']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon8 = mysqli_real_escape_string($config, $_POST['icon_input_8']);
    // This will make the connect to database and will post the data to the database.
    $facility_8 = mysqli_real_escape_string($config, $_POST['school_facility_8']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon_mission = mysqli_real_escape_string($config, $_POST['icons_mission']);
    // This will make the connect to database and will post the data to the database.
    $mission_heading = mysqli_real_escape_string($config, $_POST['school_mission_heading']);
    // This will make the connect to database and will post the data to the database.
    $mission_para = mysqli_real_escape_string($config, $_POST['school_mission_para']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon_vision = mysqli_real_escape_string($config, $_POST['icons_vision']);
    // This will make the connect to database and will post the data to the database.
    $vision_heading = mysqli_real_escape_string($config, $_POST['school_vision_heading']);
    // This will make the connect to database and will post the data to the database.
    $vision_para = mysqli_real_escape_string($config, $_POST['school_vision_para']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon_activities = mysqli_real_escape_string($config, $_POST['icons_activities']);
    // This will make the connect to database and will post the data to the database.
    $activities_heading_1 = mysqli_real_escape_string($config, $_POST['school_activities_heading_1']);
    // This will make the connect to database and will post the data to the database.
    $activities_para_1 = mysqli_real_escape_string($config, $_POST['school_activities_para_1']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon_activities_2 = mysqli_real_escape_string($config, $_POST['icons_activities_2']);
    // This will make the connect to database and will post the data to the database.
    $activities_heading_2 = mysqli_real_escape_string($config, $_POST['school_activities_heading_2']);
    // This will make the connect to database and will post the data to the database.
    $activities_para_2 = mysqli_real_escape_string($config, $_POST['school_activities_para_2']);
    // $filename variable will have the image and image name. $_FILES will upload the image to the database.
    $icon_activities_3 = mysqli_real_escape_string($config, $_POST['icons_activities_3']);
    // This will make the connect to database and will post the data to the database.
    $activities_heading_3 = mysqli_real_escape_string($config, $_POST['school_activities_heading_3']);
    // This will make the connect to database and will post the data to the database.
    $activities_para_3 = mysqli_real_escape_string($config, $_POST['school_activities_para_3']);
    $fileData = [];
    for ($i = 1; $i <= 8; $i++) {
        $fileData[$i] = [
            'filename' => $_FILES['school_image_' . $i]['name'],
            'tmp_name' => $_FILES['school_image_' . $i]['tmp_name'],
            'size' => $_FILES['school_image_' . $i]['size'],
            'extension' => strtolower(pathinfo($_FILES['school_image_' . $i]['name'], PATHINFO_EXTENSION)),
            'destination' => "upload/" . $_FILES['school_image_' . $i]['name'],
        ];
    }

    // Validate. This could be done in the storage loop, too
    if (!empty($fileData)) {
        $allow_types = ['jpg', 'png', 'jpeg', 'webp', 'svg'];
        foreach ($fileData as $index => $file) {
            if (!in_array($file['extension'], $allow_types, true)) {
                $msg = ["Image file type $index is not allowed (only jpg, jpeg, png, webp, svg)", "alert-danger"];
                $_SESSION['msg'] = $msg;
                header("location:add_blog.php");
                exit;
            }

            if ($file['size'] > 5000000) {
                $msg = ["Image file $index size should not be greater then 5MB", "alert-danger"];
                $_SESSION['msg'] = $msg;
                header("location:add_blog.php");
                exit;
            }
        }
    } else {
        $sql2 = "INSERT INTO blog(blog_title, school_tagline, school_rating, school_projects, school_events, school_mobile, school_email, blog_body, icon_input, school_facility, icon_input_2, school_facility_2, icon_input_3, school_facility_3, icon_input_4, school_facility_4, icon_input_5, school_facility_5, icon_input_6, school_facility_6, icon_input_7, school_facility_7, icon_input_8, school_facility_8, icons_activities, school_activities_heading_1, school_activities_para_1, icons_activities_2, school_activities_heading_2, school_activities_para_2, icons_activities_3, school_activities_heading_3, school_activities_para_3, icons_mission, school_mission_heading, school_mission_para, icons_vision, school_vision_heading, school_vision_para, school_activities, school_mission_vision, blog_image, school_images_1, icon_social, school_instagram, icon_social_2, school_facebook, icon_social_3, school_twitter, category, author_id) VALUES('$title', '$tagline', '$rating', '$projects', '$events', '$mobile', '$email', '$body', '$icon', '$facility_1', '$icon2', '$facility_2', '$icon3', '$facility_3', '$icon4', '$facility_4', '$icon5', '$facility_5', '$icon6', '$facility_6', '$icon7', '$facility_7', '$icon8', '$facility_8', '$icon_activities', '$activities_heading_1', '$activities_para_1', '$icon_activities_2', '$activities_heading_2', '$activities_para_2', '$icon_activities_3', '$activities_heading_3', '$activities_para_3', '$icon_mission', '$mission_heading', '$mission_para', '$icon_vision', '$vision_heading', '$vision_para',  '$activities', '$missionVision', $fileData '$social_icon', '$social_icon_2', '$social_icon_3', '$instagram', '$facebook', '$twitter',  '$category', '$author_id')";
        $query2 = mysqli_query($config, $sql2);
        if ($query2) {
            $msg = ["Post Publish Successfully sql2", "alert-success"];
            $_SESSION['msg'] = $msg;
            // If the post publish successfully then redirect to same page with success message.
            header("location:add_blog.php");
        } else {
            $msg = ["Failed, Please try again sql2", "alert-danger"];
            $_SESSION['msg'] = $msg;
            // If the post it not able to post then redirect to same page with error message.
            header("location:add_blog.php");
        }
    }
}

?>

下面是输出屏幕截图

knsnq2tg

knsnq2tg1#

您的脚本中有一个小错误...

<input type="file" name="school_image" class="form-control">

应该是...

<input type="file" name="school_image_1" class="form-control">

您忘记了_1,可能是因为这个原因而传回错误。请修正并告知我们。

相关问题