上传图片(阿贾克斯提交)

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title></title>
 <script src="jquery.js"></script>
</head>
<body>

   <form action="1.php" method="post" enctype="multipart/form-data" target="mywin">
     <input type="file" name="up_file">
     <!-- <input type="submit" name="btn" value="上传">
     <input type="hidden" name="path" value=""> -->
   </form>
   <div>
                <img   id="imgid" style=" 100px;height: 100px; display: none;" >

            </div>
     <iframe   frameborder="1"  name="mywin" style="display:none;" src=""></iframe>
   
</body>
<script>
    $(document).ready(function(){
      $(":file").change(function(){
       $("form").submit();
      });
    });
</script>
<script>
 
</script>
</html>

<?php
 header('Content-type:text/html;charset="utf8"');
 // session_start();
      if(!is_dir('uploads')){
        mkdir('uploads');
      }
      $src=$_FILES['up_file']['tmp_name'];
      $file=$_FILES['up_file']['name'];
      $a=explode('.',$file);
      $ext=array_pop($a);
      $rand=time().mt_rand().'.'.$ext;
      $dst="uploads/{$rand}";
      if($_FILES['up_file']['error']===0){
          if(move_uploaded_file($src,$dst)){
            
               echo "<script>imgid=window.top.document.getElementById('imgid');
              imgid.src='{$dst}'</script>";
          }else{
              echo "头像未上传成功";
          }
      }

    

原文地址:https://www.cnblogs.com/index0629/p/8111765.html