【问题标题】:PHP - How can I show the uploaded image instantly on the same page?PHP - 如何在同一页面上立即显示上传的图像?
【发布时间】:2018-10-20 12:14:55
【问题描述】:

以下是该网站的外观,可为您提供一个想法。

我想显示他在单击Bestand kiezen 后立即输入的图像(= 转换为选择的文件)。现在它的工作原理是它只显示文件名,我怎样才能立即显示图像的预览?图像和其他信息仅在单击添加帖子按钮后才插入数据库。

这应该用 ajax 来完成吗?

addpost.php 代码

<?php
include_once('classes/Post.class.php');
include_once('classes/User.class.php');

User::checklogin();
$post = Post::ShowPosts();

if(! empty($_POST)) {

    $file = $_FILES['file'];
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];
    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png');

    if (in_array($fileActualExt, $allowed)){
        if ($fileError === 0){
            if ($fileSize < 10000000){
                $fileNameNew = uniqid('', true).".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                print_r($fileDestination);
                move_uploaded_file($fileTmpName, $fileDestination);
                $feedback = "Post has been saved.";
                $title = $_POST['title'];
                $desc = $_POST['description'];
                $filter = $_POST['filter'];
                $date = date("Y-m-d H:i:s");
                $location = "";
                $userid = $_SESSION['userid'];
                $newPost = new Post();

                $newPost->setTitle($title);
                $newPost->setPicture($fileDestination);
                $newPost->setDescription($desc);
                $newPost->setDate($date);
                $newPost->setUserId($userid);
                $newPost->setLocation($location);
                $newPost->setFilter($filter);
                $newPost->AddPost();
                $postId = Post::getLastId();


                $string = $_POST['tag'];
                $tags = explode(',' , $string);

                foreach($tags as $t) {
                    $newPost->setTag($t);
                    $newPost->AddTags($postId);
                }

            } else{
                $feedback = "Your file is too big.";
            }
        } else{
            $feedback = "There was an error uploading your file.";
        }
    } else{
        $feedback = "You cannot upload files of this type.";
    }



}
?><!DOCTYPE html>
<html lang="en">

<body>


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

    <h1 form__title>Add post</h1>
    <?php  if(isset($feedback)): ?>
        <div class="feedback">
            <p><?php echo $feedback; ?></p>
        </div>
    <?php endif; ?>
    <div class="form__field">
        <label for="title" class="label">YOUR SHOT TITLE:</label> <br/>
        <input class="form__input" type="text" name="title">
    </div>
    <div class="form__field">
        <label for="file" class="label">UPLOAD PICTURE</label><br/>
        <input class="form__input" type="file" name="file" class="fileToUpload">
    </div>
    <div class="form__field">
        <label for="description" class="label">DESCRIPTION</label><br/>
        <textarea name="description" cols="25" rows="5"></textarea>
    </div>

    <div class="form__field">
        <label for="tag" class="label">ADD SOME TAGS TO YOUR SHOT (seperated with , )</label><br/>
        <input class="form__input" type="text" name="tag">
    </div>

    <p>JPG, GIF or PNG. Snapshots are 400 × 300 pixels or 800 × 600 (for HiDPI displays). </p><br/><br/>

    <div class="form__field">
        <label for="tag" class="label">ADD ONE (Instagram) FILTER TO YOUR SHOT </label><br/>

        <select name="filter">
            <option value="_1977">1977</option>
            <option value="aden">aden</option>
            <option value="xpro2">xpro2</option>
        </select>
    </div>

    <div class="form__field">
        <input class="btn_style" type="submit" name="submit" value="Add post"">
    </div>
</form>


</body>
</html>

Addpost 函数

public function AddPost(){

        $conn = db::getInstance();
        $query = "insert into posts (post_title, picture, description, filter, location, user_id, post_date) values (:post_title, :picture, :description, :filter, :location,  :user_id, :post_date)";
        $statement = $conn->prepare($query);
        $statement->bindValue(':post_title',$this->getTitle());
        $statement->bindValue(':picture',$this->getPicture());
        $statement->bindValue(':description',$this->getDescription());
        $statement->bindValue(':filter', $this->getFilter());
        $statement->bindValue(':location',$this->getLocation());
        $statement->bindValue(':user_id',$this->getUserId());
        $statement->bindValue(':post_date',$this->getDate());
        $statement->execute();
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

【问题讨论】:

标签: php ajax database oop file-upload


【解决方案1】:

HTML - Display image after selecting filename

用javascript sn-p很好地回答了这个问题,看第一个答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多