核心思想:通过Html的iframe标签属性操作*窗口,再用php动态无刷新上传图片文件。
示例如下:
demo
|------uploads #存放上传的文件
|------index.php
|------upload.php
|------jquery.js
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<script src="jquery.js"></script>
</head>
<body>
<h1>
<?php
echo "当前时间戳:".time();
?>
</h1>
<form action="upload.php" target='myform' method="post" enctype="multipart/form-data">
<p>文件上传:</p>
<p>
<input type="file" name="img">
</p>
</form>
<div>
<img src="" alt="" id="imgid">
</div>
<iframe name ="myform" frameborder="1" src="" style="display:none"></iframe>
</body>
<script>
$(':file').change(function(){
$('form').submit();
});
</script>
</html>
upload.php
<?php
error_reporting(0);
$src=$_FILES['img']['tmp_name'];
$file=$_FILES['img']['name'];
$ext=array_pop(explode('.', $file));
$rand=time().mt_rand().'.'.$ext;
//echo $rand;
$rst="uploads/{$rand}";
if ($_FILES['img']['error']===0) {
if(move_uploaded_file($src, $rst)){
echo "<script>top.document.getElementById('imgid').src='{$rst}'</script>";
}
}
?>