【发布时间】:2014-03-22 16:15:14
【问题描述】:
如何在点击上传按钮后立即显示图片?
这是我的 HTML 代码:
form.php
<html>
<body>
<form action="uploadFile.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
这是我的uploadFile.php代码:(已更新,但仍然没有得到任何结果)
<?php
// prevent timezone warnings
date_default_timezone_set('Philippines/Manila');
// set the upload location
$UPLOADDIR = "images/";
// if the form has been submitted then save and display the image(s)
if(isset($_POST['Submit'])){
// loop through the uploaded files
foreach ($_FILES as $key => $value){
$image_tmp = $value['tmp_name'];
$image_type=$value['type'];
$image = $value['name'];
$image_file = "{$UPLOADDIR}{$image}";
//check if there's existing file name
if ($image != 0){
echo 'File Already Exists!';
}
else {
// move the file to the permanent location
if(move_uploaded_file($image_tmp,$image_file)){
// this is where the displaying part goes
echo <<<HEREDOC
<div style="float:left;margin-right:10px">
<img src="$editedShear" alt="This is your image" height=200 width=200/></br>
</div>
HEREDOC;
}
else{
echo "<h1>image file upload failed, image too big after compression</h1>";
}
}
}}
else{
?>
<?php
}
?>
【问题讨论】:
-
你必须使用ajax请求,这意味着你已经使用ajax进行了上传,成功后你可以显示图像
-
提交后是否出现错误或看到空白页?
-
不,只是一个损坏的图像。与图像的描述。这就是我在提交按钮后得到的
-
旁注,我知道这个问题已经有几天了,但是您使用
$editedShear作为图像源,并且没有在任何地方声明它。调试时可能有助于打开错误报告:)
标签: php