用户可以上传图片,并将其插入到可信任的div中。
当用户上传一张图片时,页面会刷新,图像会显示在contenteditable div中。当他们然后添加另一个图像时,第一个图像会被自动删除。
如何在不上传其他图像的情况下显示所有图像?
<?php
if(isset($_POST['upload']))
{
$ImageName = $_FILES['photo']['name'];
$fileElementName = 'photo';
$path = '../photos/';
$location = $path . $_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'], $location);
header('Location: newpost.php?img='.$location);
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="photo" onchange="document.getElementById('upload').click();" id="file" class="inputfile" />
<input type="submit" style="display: none;" name="upload" id="upload">
<div id="post" contenteditable>
if(!empty($_GET['img'])) {
echo'<img src="'.$_GET['img'].'" class="image" />';
}
?>
</div>
</form>
答案 0 :(得分:0)
最简单的解决方案是使用会话变量。
<?php
session_start();
if(isset($_POST['upload']))
{
$ImageName = $_FILES['photo']['name'];
$fileElementName = 'photo';
$path = '../photos/';
$location = $path . $_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'], $location);
$imageArray = array();
if (isset($_SESSION['images'])){
$imageArray = $_SESSION['images'];
}
array_push($imageArray,$location);
header('Location: newpost.php?img='.$location);
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="photo" onchange="document.getElementById('upload').click();" id="file" class="inputfile" />
<input type="submit" style="display: none;" name="upload" id="upload">
<div id="post" contenteditable>
if(isset($_SESSION['images'])) {
foreach($_SESSION['images'] as $image){
echo'<img src="'.$image .'" class="image" />';
}
}
?>
</div>
</form>