Home >Backend Development >PHP Tutorial >Hide iframe to upload files without refreshing, iframe to upload files with refresh_PHP tutorial

Hide iframe to upload files without refreshing, iframe to upload files with refresh_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:58:05691browse

Hide iframe to upload files without refreshing, iframe to upload files with refresh

First of all, ajax cannot upload files, which misled me for a while. I couldn’t sleep tonight so I followed the instructions. Upload files without refreshing

In fact, the principle is very simple

<form enctype="multipart/form-data" method="POST" target="upload" action="http://localhost/class.upload.php" ><br />          <input type="file" name="uploadfile" /><br />          <input type="submit" /> <br /></form><br /><iframe name="upload"><img src="http://www.bkjia.com/uploads/allimg/160303/1A0226225-0.gif" alt="Hide iframe to upload files without refreshing, iframe to upload files with refresh_PHP tutorial" /></span>
<pre class="code">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 />   }
}
Hide iframe to upload files without refreshing, iframe to upload files with refresh_PHP tutorial

Call example:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1105144.htmlTechArticleHide iframe to upload files without refreshing, iframe to upload files with refresh. First, ajax cannot upload files, which misled me for a while. , I couldn’t sleep tonight, so I followed the instructions and uploaded the file without refreshing...
Statement:
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