html代碼
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html;charset=utf-8">
- <title>js和iframe實現圖片無刷新上傳</title>
- <style type="text/css">
- #h1{
- text-align:center;
- }
- <!--隱藏iframe-->
- iframe{
- display:none;
- }
- </style>
- </head>
- <body>
- <h1 id='h1'>js和iframe實現圖片無刷新上傳</h1>
- <h1 id='h1'><script>document.write(new Date())</script></h1>
- <form action="./file.php" method="post" enctype="multipart/form-data" target="upload">
- <input type="file" name="file" id="file"/>
- <br>
- <input type="submit" name="sub" value="提交"/>
- </form>
- <img id="imgid" name="imgid">
- <iframe src="" name="upload">
- </iframe>
- </body>
- <script>
- </script>
- </html>
php代碼
- <?php
- $sfile=$_FILES['file']['tmp_name'];
- $dfile=$_FILES['file']['name'];
- if(move_uploaded_file($sfile,$dfile)){
- echo "<script>imgobj=window.top.document.getElementById('imgid');imgobj.src='$dfile'</script>";
- }