欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php利用iframe实现无刷新文件上传功能的代码

程序员文章站 2022-10-05 20:27:13
复制代码 代码如下:      无刷新上传文件   
复制代码 代码如下:

<html>
  <head>
  <title>无刷新上传文件</title>
  <meta content-type="text/html" charset="utf-8" />
  <script type="text/网页特效">
  function startupload() {
  document.getelementbyid('processing').innerhtml = 'loding...';
  return true;
  }
  function stopupload(rel){
  var msg;
  switch (rel) {
  case 0:
  msg = "上传成功";
  break;
  case 1:
  msg = "上传的文件超过限制";
  break;
  case 2:
  msg = "只能上传图片文件";
  break;
  default:
  msg = "上传文件失败";
  }
  document.getelementbyid('processing').innerhtml = msg;
  }
  </script>
  </head>
  <body>
  <div style="text-align:center">
  <div id="processing"></div>
  <form action="upload.php教程" method="post" enctype="multipart/form-data" target="form-target" onsubmit="startupload();">
  <input type="hidden" name="max_file_size" value="1000000" />
  <input type="file" name="myfile" />
  <input type="submit" name="sub" value="upload" />
  </form>
  <iframe style="width:0; height:0; border:0;" name="form-target"></iframe>
  </div>
  </body>
  </html>
  php代码
  <?php
  sleep(2);
  $filetypes = array('jpg','png','gif','bmp');
  $result = null;
  $uploaddir = './upfiles';
  $maxsize = 1 * pow(2,20);
  if ($_server['request_method'] == 'post' && isset($_post['sub'])) {
  $myfile = $_files['myfile'];
  $myfiletype = substr($myfile['name'], strrpos($myfile['name'], ".") + 1);
  if ($myfile['size'] > $maxsize) {
  $result = 1;
  } else if (!in_array($myfiletype, $filetypes)) {
  $result = 2;
  } elseif (is_uploaded_file($myfile['tmp_name'])) {
  $tofile = $uploaddir . '/' . $myfile['name'];
  if (@move_uploaded_file($myfile['tmp_name'], $tofile)) {
  $result = 0;
  } else {
  $result = -1;
  }
  } else {
  $result = 1;
  }
  }
  ?>
  <script type="text/javascript">
  window.top.window.stopupload(<?php echo $result; ?>);
  </script>