<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片上传</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
当前时间:
<?php echo date("H:i:s",time()); ?>
<form action="upload.php" method="post" enctype="multipart/form-data" target="mywin">
上传图片 <input type="file" name="img"></input>
</form>
<div>
<img src="" id="imgid" > </div>
<iframe frameborder="1" name="mywin" style="display: none;" src=""></iframe>
</body>
<script type="text/javascript">
$(document).ready(function(){
$(':file').change(function(){
$('form').submit();
});
}); </script>
</html>
<?php
$src=$_FILES['img']['tmp_name'];
$file=$_FILES['img']['name'];
$a=explode('.',$file);
$ext=array_pop($a);
$rand=time().mt_rand().'.'.$ext;
$dst="uploads/{$rand}";
if($_FILES['img']['error']===){
if(move_uploaded_file($src,$dst)){
//获取最顶层浏览器
echo "<script>imgid=window.top.document.getElementById('imgid');
imgid.src='{$dst}'</script>";
}
}
目录下建立一个uploads文件夹存储上传图片