【问题标题】:Every time I set my form to enctype="multipart/form-data" the file upload data doesn't go into the database每次我将表单设置为 enctype="multipart/form-data" 时,文件上传数据都不会进入数据库
【发布时间】:2017-10-15 05:28:25
【问题描述】:

每次我将表单设置为 enctype="multipart/form-data" 时,文件上传数据都不会进入数据库。除了设置为 VARCHAR(255) 的文件上传数据外,其他所有字段都进入数据库。这是我的表格:

<form method="post" action="post.php" enctype="multipart/form-data">
<label for="post_title">Title</label>
<br>
<input type="text" name="post_title" id="post_title" required autofocus>
<br>
<label for="post_content">Content</label>
<br>
<textarea name="post_content" id="post_content" required>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a risus ligula. Donec suscipit purus vel euismod feugiat. Suspendisse potenti. Praesent rhoncus mauris urna. Vestibulum non nisi quis sem posuere tempor. Morbi placerat tellus risus, et vestibulum ipsum fermentum ut. Ut faucibus aliquam augue nec laoreet. Fusce lobortis vestibulum tempor. Duis pharetra eget nisi eu dictum. Quisque sed ante eget ipsum lacinia cursus porta ut ante. Morbi venenatis elementum massa nec auctor. Ut id luctus dolor, at viverra arcu. Nulla auctor molestie pharetra.</textarea>
<script>
    CKEDITOR.replace('post_content');
</script>
<br>
<label for="new_category">New Category</label>
<br>
<input type="text" name="new_category" id="new_category">
<br>
<label for="choose_category">Choose Category</label>
<br>
<select name="choose_category" id="choose_category">
<?php
while($column = mysqli_fetch_assoc($result)){
?>
<option value="<?php echo $column['post_category'] ?>"><?php echo $column["post_category"] ?></option>
<?php
}
?>
</select>
<br>
<label for="post_date">Date</label>
<br>
<input type="date" name="post_date" id="post_date" value="<?php echo $current_date ?>" required>
<br>
<label for="post_image">Featured Image</label>
<br>
<input type="file" name="post_image" id="post_image">
<br>
<input type="submit" value="Submit" id="submit">
</form>

知道为什么我提交后 post_image 没有在数据库中显示任何数据吗?这是PHP:

<?php

$current_date = date("Y-m-d");

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

$post_title = isset($_POST['post_title']) ? $_POST['post_title'] : null;
$post_content = isset($_POST['post_content']) ? $_POST['post_content'] : null;
if($_POST['new_category']==""){
$post_category = ($_POST['choose_category']);
}else{
$post_category = ($_POST['new_category']);
}
$post_date = isset($_POST['post_date']) ? $_POST['post_date'] : null;
$post_image = isset($_POST['post_image']) ? $_POST['post_image'] : null;

$sql = "INSERT INTO posts (post_title, post_content, post_category, post_date, post_image, user_name) VALUES ('$post_title', '$post_content', '$post_category', '$post_date', '$post_image', '$user_name')";

$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));

if(!$result){
        die(mysqli_error($connection));
        }else{
        $s_post="Posted successfully.";
        }

}

?>

【问题讨论】:

  • 显示php端
  • @Akintunde 我刚刚添加了 PHP。
  • 你应该使用$_FILES而不是$_POST访问php中的文件
  • @Akintunde 我将该行更改为$post_image = isset($_FILES['post_image']) ? $_FILES['post_image'] : null;,但现在它显示“注意:第 19 行 C:\wamp\www\blog\post.php 中的数组到字符串转换”。

标签: php file-upload


【解决方案1】:

您阅读过 PHP 文件上传文档吗? http://php.net/manual/en/features.file-upload.php

$_FILES 是一个数组,它包含上传文件的元数据和上传临时文件的路径。 您必须处理所有上传的文件并将它们存储在您的应用程序中。

你应该在你的 PHP 脚本中包含这样的内容:

<?php

// ...

if (!empty($_FILES['post_image'])) {
    $filepath = '/path/to/uploads/directory/' . basename($_FILES['post_image']['name']);

    if (move_uploaded_file(
        $_FILES['post_image']['tmp_name'],
        $filepath
    )) {
        echo 'File stored in ' . $filepath;
    } else {
        throw new Exception('File could not be stored');
    }

    // Here you can store $filepath in database
    // ...
}

请注意,您应该添加一些验证和保护以防止常见的上传攻击。

【讨论】:

猜你喜欢
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2011-02-12
  • 1970-01-01
  • 2016-04-02
相关资源
最近更新 更多