php图像上传并显示在同一个div中

时间:2015-08-13 05:21:44

标签: php

我想将图像上传到目录,我希望在div中同时显示它。这是我的html页面的一部分。

<body>
  <div id="imgspace1" class="container">
  <form action="php/imgUpload.php" method="POST" enctype="multipart/form-data"> 
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload" name="Submit">
  </form>
  </div>

这是我的php页面

<?php
// set the upload location
$imgUpload = "../uploads/";

// if the form has been submitted then save and display the image(s)
if(isset($_POST['Submit'])){
    // loop through the uploaded file
    foreach ($_FILES as $key => $value){
        $image_tmp = $value['tmp_name'];
        $image = $value['name'];
        $image_file = "{$imgUpload}{$image}";
        // move the file to the permanent location
        if(move_uploaded_file($image_tmp,$image_file)){
            echo <<<HEREDOC
<div style="float:left;margin-right:10px">
    <img src="{$image_file}" alt="file not found" /></br>
</div>
HEREDOC;
        }
        else{
            echo "<h1>image file upload failed, image too big after compression</h1>";           
        }
    }
}
else{
    ?>

<?php
}
?>

但图像显示在另一页面中。那么如何在“imgspace1”div中显示上传的图像?

1 个答案:

答案 0 :(得分:0)

此示例演示了您的问题的解决方案

<iframe width="100%" height="300" src="//jsfiddle.net/chintan7027/s8nvgnsn/2/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>