如何运用PHP Ajax实现图片的无刷新上传

2024-12-17 17:59:20
推荐回答(1个)
回答1:

      
            
              指定文件名:     
               

  
                 上传文件:   
                   
                   
              

    
                  
            
          
          
          
        function showPreview(source) {  
            var file = source.files[0];  
            if (window.FileReader) {  
                var fr = new FileReader();  
                fr.onloadend = function(e) {  
                    document.getElementById("portrait").src = e.target.result;  
                };  
                fr.readAsDataURL(file);  
            }  
        }  
      
    if($_FILES['photo']['error']>0){  
        echo "上传文件失败";  
        die;  
    }  
    $dir='./photo/';  
    $type=substr($_FILES['photo']['name'],strrpos($_FILES['photo']['name'],'.'));  
    $filename=time().rand(1000,9999).$type;  
    if(is_uploaded_file($_FILES['photo']['tmp_name'])){  
        move_uploaded_file($_FILES['photo']['tmp_name'],$dir.$filename);  
        echo "上传成功";  
    }else{  
        echo "上传文件失败";  
    }