zoukankan      html  css  js  c++  java
  • iframe无刷新上传文件

    form.html

    <form enctype="multipart/form-data" method="post" target="upload" action="upload.php" > 
    <input type="file" name="uploadfile" />
    <input type="submit" /> 
    </form> 
    <iframe name="upload" style="display:none"></iframe> 
    
    <!--和一般的<form>标签相比多了一个target属性罢了,用于指定标签页在哪里打开以及提交数据。 
    
    如果没有设置该属性,就会像平常一样在本页重定向打开action中的url。 
    
    而如果设置为iframe的name值,即"upload"的话,就会在该iframe内打开,因为CSS设置为隐藏,因而不会有任何动静。若将display:none去掉,还会看到服务器的返回信息。 
    -->

    upload.php

    <?php
    header("Content-type:text/html;charset=utf-8");
    class upload{
        public $_file;
        
        public function __construct(){
            if(!isset($_FILES['uploadfile'])){
                $name=key($_FILES);
            }
            if(!isset($_FILES['uploadfile'])){
                throw new Exception("并没有文件上传"); 
            }
            
            $this->_file=$_FILES['uploadfile']; //$this->_file一维数组
            var_dump($this->_file);
    
            //判断文件是否是通过 HTTP POST 上传的
            //如果 filename 所给出的文件是通过 HTTP POST 上传的则返回 TRUE。这可以用来确保恶意的用户无法欺骗脚本去访问本不能访问的文件,例如 /etc/passwd。 
            if(!is_uploaded_file($this->_file['tmp_name'])) 
                throw new Exception("异常情况"); 
    
            if($this->_file['error'] !== 0) 
                throw new Exception("错误代码:".$this->_file['error']); 
        }
        
        public function moveTo($new_dir){
            $real_dir=$this->checkDir($new_dir).'/';
            $real_dir=str_replace("\\","/",$real_dir);
            if(!move_uploaded_file($this->_file['tmp_name'],$real_dir.$this->_file['name'])){
                exit('上传失败');
            }
            echo "<script type='text/javascript'>alert('上传成功')</script>";
        }
        
        public function checkDir($dir){
            if(!file_exists($dir)){
                mkdir($dir,0777,true);
            }
            return realpath($dir);    
        }
    }
    
    $upload=new upload();
    $new_dir="./a/b";
    $upload->moveTo($new_dir);
    
    
    
    
    
    
    ?>
  • 相关阅读:
    pop指令的执行过程
    ipad 4.2 开发环境
    XP远程桌面
    停机问题、哥德尔定理
    看MIT的线性代数
    VizA项目继续推进
    port systemc to mac os x
    关于jar
    普桑
    拉突爾
  • 原文地址:https://www.cnblogs.com/loveyouyou616/p/2848717.html
Copyright © 2011-2022 走看看