【问题标题】:How to set a already uploaded image as default input(type=file) in the html form?如何将已上传的图像设置为 html 表单中的默认输入(类型=文件)?
【发布时间】:2020-05-22 03:48:42
【问题描述】:

我想更新我的数据库表中的图像。所以首先我做了一个 MySQL 查询以在 HTML 表单网页中显示已经上传的图像。在那之后,我又做了一个查询来更新图片......所以现在我想要如果我没有为更新选择任何图片,那么应该选择 已经上传的图片作为表单中的默认输入.那么如何将已经上传的图片设置为表单中的默认输入呢?请帮帮我...

<img src="../images/<?php echo $post_image ?>" width="100px">
<input type="file" name="image" class="form-control-file">

<!-------------------Display Image--------------------->
<?php

    $query = "SELECT * FROM posts WHERE post_id=$post_id ";
    $getPost = mysqli_query($connection, $query);

    while ($row = mysqli_fetch_assoc($getPost)) {
        $post_image = $row['post'];
    }    
        
?>

<!------------------Update Image------------------->
<?php 

if (isset($_POST['publish_edit'])) {
    $post_image = $_FILES['image']['name'];
    $post_image_temp = $_FILES['image']['tmp_name'];

    move_uploaded_file($post_image_temp, "../images/$post_image");
    
    $query = "UPDATE posts SET ";
    $query .= "post_image = '$post_image' ";
    $query .= "WHERE post_id = $post_id ";
    
    $editPost = mysqli_query($connection, $query);
    }

?>

【问题讨论】:

  • 在 HTML 中这样做是不可能的。如果是这样,页面可能会自动从任何用户的磁盘上传文件,这将是一个巨大的安全隐患。

标签: javascript php html mysql database


【解决方案1】:

检查图像输入是否为空。如果为空,则将图像数据库列保留为之前的状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2018-10-03
    • 1970-01-01
    相关资源
    最近更新 更多