Home > Backend Development > PHP Tutorial > 暗藏iframe无刷新上传文件

暗藏iframe无刷新上传文件

WBOY
Release: 2016-06-13 12:28:44
Original
799 people have browsed it

隐藏iframe无刷新上传文件

首先ajax不能上传文件,这误导了我有段时间,今晚睡不着就照着说明做了个无刷新上传文件

其实原理很简单

Copy after login




和一般的

标签相比多了一个target属性罢了,用于指定标签页在哪里打开以及提交数据。

如果没有设置该属性,就会像平常一样在本页重定向打开action中的url。

而如果设置为iframe的name值,即"upload"的话,就会在该iframe内打开,因为CSS设置为隐藏,因而不会有任何动静。若将display:none去掉,还会看到服务器的返回信息。

另外贴一下自己组织的类。

复制代码
class upload<br>{<br>   public $_file;<br><br>   public function __construct( $name =null)<br>   {<br>       if(is_null($name) || !isset($_FILES[$name]))<br>            $name = key($_FILES);<br>            <br>       if(!isset($_FILES[$name]))<br>           throw new Exception("并没有文件上传");<br>           <br>       $this->_file  = $_FILES[$name];<br>       <br>       if(!is_uploaded_file($this->_file['tmp_name']))<br>            throw new Exception("异常情况");<br>       if($this->_file['error'] !== 0)<br>            throw new Exception("错误代码:".$this->_file['error']);     <br>   }<br>   public function moveTo( $new_dir)<br>   {<br>       $real_dir = $this->checkDir($new_dir);<br>       return move_uploaded_file($this->_file['tmp_name'], $real_dir.'/'.$this->_file['name']);<br>   }<br>   private function checkDir($dir)<br>   {<br>       $real_dir = realpath($dir);<br>       if($real_dir === false)<br>           throw new Exception("给定目录{$dir}不存在");<br>       if(!is_writable($real_dir))<br>           throw new Exception("给定目录{$dir}不可写");<br>       return $real_dir;<br>   }
Copy after login
}
Copy after login
复制代码

调用示例:

$inputName =  'uploadfile'; <br> // 即<input type='“file"' name="uploadfile"> 中的name值,不填也行<br>$upload = new upload($inputName);<br>$new_dir = "/www";  // 将文件移动到的路径<br>$upload->moveTo($new_dir);
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template