文件上传器不起作用。我想念什么?

时间:2018-09-19 14:28:49

标签: php html mysql file-upload

我正在构建文件(图像)上传表单。除了我的代码的这一部分之外,其他所有东西都运行正常:

uploader.php(摘录)

<label class="slds-form-element__label" for="pressoInput">Presso</label>
<div class="slds-form-element__control">
  <input id="pressoInput" class="slds-input" type="text" />
</div>
...
<label class="slds-form-element__label" for="regionInput">Region</label>
<div class="slds-form-element__control">
  <input id="regionInput" class="slds-input" type="text" disabled="disabled" />
</div>

我真的不明白。我已经运行了语法检查,以确保我没有遗漏诸如括号之类的明显内容,但是我找不到任何东西。你们看到什么了吗?这是完整的文件:

uploader.php

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

这是数据库连接文件:

db.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();

?>

最后是索引(主)文件:

index.php

<?php

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

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

?>

0 个答案:

没有答案