php文件上传程序无法工作我错过了什么?

fsi0uk1n  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(311)

这个问题在这里已经有答案了

如何显示mysqli查询的错误[重复](2个答案)
引用-这个错误在php中是什么意思(36个答案)
两年前关门了。
我正在建立一个文件(图像)上传表单。除了我的代码的这一部分外,一切都运行良好:
uploader.php(节选)

if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "Sorry, something went wrong. Please try again later.";
}

我真的不明白。我已经运行语法检查,以确保我没有遗漏一些明显的东西,如大括号或什么,但我找不到任何东西。你们看到什么了吗?以下是完整文件:
上载程序.php

<?php

if (isset($_POST['submit'])) {

    // Collecting the original data
    $name = $_POST['name'];
    $description = $_POST['description'];
    $file = $_FILES['file'];

    // Creating new variables for the collected data
    $fileNameExp = explode(".", $file['name']);
    $fileTypeExp = explode("/", $file['type']);
    $fileTemp = $file['tmp_name'];
    $fileError = $file['error'];
    $fileSize = $file['size'];

    // Creating a variable for the file name (without extension)
    $fileName = reset($fileNameExp);

    // Creating a variable for the file extension
    $fileExtension = end($fileTypeExp);

    // Creating an array for the allowed extensions
    $allowedExtensions = array("jpg", "jpeg", "png");

    // Creating a new file name for the database record
    $fileName = reset($fileNameExp) . "." . uniqid("", true) . "." . $fileExtension;
    echo $fileName . "<br />";

    // Creating a destination variable for the file
    $fileDestination = "/images/" . $fileName;

    // Making sure the data is safe and usable
    if (empty($name)) {
        echo "Please tell others (at least) your first name";
    } elseif (empty($description)) {
        echo "Please tell others a bit about you";
    } elseif ($fileSize <= 0) {
        echo "Please upload a picture of you";
    } elseif ($fileError > 0) {
        echo "Sorry, there's been an error. Please try again";
    } elseif (!in_array($fileExtension, $allowedExtensions)) {
        echo "You can't upload a " . $fileExtension . " image. (extensions allowed: jpg, jpeg and png)";
    } elseif ($fileSize <= 20000) {
        echo "Please upload a smaller picture (the maximum size is 20 MB)";
    } else {
        echo "Amazing! Your picture was published. You're gonna be redirected shortly. Otherwise, click here.";

        include "db.php";

        $sql = "SELECT * FROM gallery;";
        $stmt = mysqli_stmt_init($conn);

        if (!mysqli_stmt_prepare($stmt, $sql)) {
            echo "Sorry but something went wrong. Please try again later.";
        } else {
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            $rowCount = mysqli_num_rows($result);
            $order = $rowCount + 1;

            $sql = "INSERT INTO gallery (name, description, imageName) VALUES (?, ?, ?);";

            if (!mysqli_stmt_prepare($stmt, $sql)) {
                echo "Sorry, something went wrong. Please try again later.";
            } else {
                mysqli_stmt_bind_param($stmt, "sss", $name, $description, $imageName);
                mysqli_stmt_execute($stmt);

                move_uploaded_file($fileTemp, $fileDestination);

                header("Location: /index.php?upload=success");
            } exit();
        } exit();
    } exit();
} else {
        echo "You're not allowed to see this page. Sorry bro.";
} exit();

?>

以下是数据库连接文件:
数据库.php

<?php

$server = "localhost";
$username = "root";
$password = "root";
$db = "hotw2";

$conn = mysqli_connect($server, $username, $password, $db);

?>

最后是索引(主)文件:
索引.php

<div>
    <?php
        include_once "db.php";

        $sql = "SELECT * FROM gallery ORDER BY id DESC";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            echo "Sorry but something  wrong. Please try again later.";
        } else {
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);

            while ($row = mysqli_fetch_assoc($result)) {
            echo "
                <div style='background-image:url(/images/" . $row['name'] . ");'></div>
                <h3>" . $row['name'] . "</h3>
                <p>" . $row['description'] . "</p>
            ";
            }
        }
    ?>
</div>

<form action="uploader.php" method="post" enctype="multipart/form-data">
    <input type="text" name="name" placeholder="Type your name here...">
    <br />
    <textarea type="text" name="description" cols="30" rows="10" placeholder="Tell others a bit about yourself..."></textarea>
    <br />
    <input type="file" name="file">
    <br />
    <button type="submit" name="submit">SUBMIT</button>
</form>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题