gpt4 book ai didi

php - 图片/文件上传不适用于 materializecss 框架

转载 作者:搜寻专家 更新时间:2023-10-31 21:21:20 25 4
gpt4 key购买 nike

我想创建一个使用数据库的帖子创建器,但我无法上传图像或文件。我的网页正在使用物化框架。此代码大部分已缩减为重要部分。

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

最佳答案

来自

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

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

这里需要添加一个表单属性enctype来发送文件。

关于php - 图片/文件上传不适用于 materializecss 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47349909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com