图像/文件上载不适用于materializecss框架

时间:2017-11-17 11:44:21

标签: php html post file-upload

我想创建一个与数据库一起使用的帖子创建者,但我无法上传图片或文件。我的网页正在使用materialize框架。这段代码大多缩小到重要部分。

<?php
require_once 'include/Database.php';
require_once 'include/Sessions.php';
require_once 'include/Functions.php';

if (isset($_POST['addPostButton']) || isset($_FILES['image'])) {

    $imageName = $_POST['image'];
    $imageDirectory = 'uploads/' . basename($_FILES['image']['name']);

    if (!move_uploaded_file($_FILES['image']['tmp_name'], $imageDirectory)) {
        $_SESSION['ErrorMessage'] = 'File is invalid!';
    } else {
        global $ConnectingDB;
        $stmt = $ConnectingDB->prepare("INSERT INTO posts(image) VALUES('$imageName')");
        if ($stmt->execute()) {
            $_SESSION['SuccessMessage'] = 'Post created';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Dashboard</title>
    <link type="text/css" rel="stylesheet" href="css/materialize.css">
    <link type="text/css" rel="stylesheet" href="css/styles.css">
</head>
<body>
    <form action="add-post.php" method="post">
        <div class="row">
            <div class="file-field input-field">
                <div class="btn green">
                    <span>File</span>
                    <input type="file" name="image">
                </div>
                <div class="file-path-wrapper">
                    <input type="text" class="file-path" placeholder="Choose an image">
                </div>
            </div>
        </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

<form action="add-post.php" method="post">

<form action="add-post.php" method="post" enctype="multipart/form-data">

此处需要向已发送文件添加表单属性enctype