php+iframe實(shí)現(xiàn)隱藏?zé)o刷新上傳文件
更新時(shí)間:2012年02月10日 16:34:06 作者:
首先ajax不能上傳文件,這誤導(dǎo)了我有段時(shí)間,今晚睡不著就照著說明做了個(gè)無刷新上傳文件
首先ajax不能上傳文件,這誤導(dǎo)了我有段時(shí)間,今晚睡不著就照著說明做了個(gè)無刷新上傳文件
其實(shí)原理很簡(jiǎn)單
<form enctype="multipart/form-data" method="POST" target="upload" action="http://localhost/class.upload.php" >
<input type="file" name="uploadfile" />
<input type="submit" />
</form>
<iframe name="upload" style="display:none"></iframe>
和一般的<form>標(biāo)簽相比多了一個(gè)target屬性罷了,用于指定標(biāo)簽頁在哪里打開以及提交數(shù)據(jù)。
如果沒有設(shè)置該屬性,就會(huì)像平常一樣在本頁重定向打開action中的url。
而如果設(shè)置為iframe的name值,即"upload"的話,就會(huì)在該iframe內(nèi)打開,因?yàn)镃SS設(shè)置為隱藏,因而不會(huì)有任何動(dòng)靜。若將display:none去掉,還會(huì)看到服務(wù)器的返回信息。
另外貼一下自己組織的類。
class upload
{
public $_file;
public function __construct( $name =null)
{
if(is_null($name) || !isset($_FILES[$name]))
$name = key($_FILES);
if(!isset($_FILES[$name]))
throw new Exception("并沒有文件上傳");
$this->_file = $_FILES[$name];
if(!is_uploaded_file($this->_file['tmp_name']))
throw new Exception("異常情況");
if($this->_file['error'] !== 0)
throw new Exception("錯(cuò)誤代碼:".$this->_file['error']);
}
public function moveTo( $new_dir)
{
$real_dir = $this->checkDir($new_dir);
return move_uploaded_file($this->_file['tmp_name'], $real_dir.'/'.$this->_file['name']);
}
private function checkDir($dir)
{
$real_dir = realpath($dir);
if($real_dir === false)
throw new Exception("給定目錄{$dir}不存在");
if(!is_writable($real_dir))
throw new Exception("給定目錄{$dir}不可寫");
return $real_dir;
}}
調(diào)用示例:
$inputName = 'uploadfile';
// 即<input type=“file" name="uploadfile" /> 中的name值,不填也行
$upload = new upload($inputName);
$new_dir = "/www"; // 將文件移動(dòng)到的路徑
$upload->moveTo($new_dir);
其實(shí)原理很簡(jiǎn)單
復(fù)制代碼 代碼如下:
<form enctype="multipart/form-data" method="POST" target="upload" action="http://localhost/class.upload.php" >
<input type="file" name="uploadfile" />
<input type="submit" />
</form>
<iframe name="upload" style="display:none"></iframe>
和一般的<form>標(biāo)簽相比多了一個(gè)target屬性罷了,用于指定標(biāo)簽頁在哪里打開以及提交數(shù)據(jù)。
如果沒有設(shè)置該屬性,就會(huì)像平常一樣在本頁重定向打開action中的url。
而如果設(shè)置為iframe的name值,即"upload"的話,就會(huì)在該iframe內(nèi)打開,因?yàn)镃SS設(shè)置為隱藏,因而不會(huì)有任何動(dòng)靜。若將display:none去掉,還會(huì)看到服務(wù)器的返回信息。
另外貼一下自己組織的類。
復(fù)制代碼 代碼如下:
class upload
{
public $_file;
public function __construct( $name =null)
{
if(is_null($name) || !isset($_FILES[$name]))
$name = key($_FILES);
if(!isset($_FILES[$name]))
throw new Exception("并沒有文件上傳");
$this->_file = $_FILES[$name];
if(!is_uploaded_file($this->_file['tmp_name']))
throw new Exception("異常情況");
if($this->_file['error'] !== 0)
throw new Exception("錯(cuò)誤代碼:".$this->_file['error']);
}
public function moveTo( $new_dir)
{
$real_dir = $this->checkDir($new_dir);
return move_uploaded_file($this->_file['tmp_name'], $real_dir.'/'.$this->_file['name']);
}
private function checkDir($dir)
{
$real_dir = realpath($dir);
if($real_dir === false)
throw new Exception("給定目錄{$dir}不存在");
if(!is_writable($real_dir))
throw new Exception("給定目錄{$dir}不可寫");
return $real_dir;
}}
調(diào)用示例:
復(fù)制代碼 代碼如下:
$inputName = 'uploadfile';
// 即<input type=“file" name="uploadfile" /> 中的name值,不填也行
$upload = new upload($inputName);
$new_dir = "/www"; // 將文件移動(dòng)到的路徑
$upload->moveTo($new_dir);
您可能感興趣的文章:
- PHP+iframe模擬Ajax上傳文件功能示例
- PHP+iFrame實(shí)現(xiàn)頁面無需刷新的異步文件上傳
- php利用iframe實(shí)現(xiàn)無刷新文件上傳功能的代碼
- php表單文件iframe異步上傳實(shí)例講解
- php+js iframe實(shí)現(xiàn)上傳頭像界面無跳轉(zhuǎn)
- PHP+iframe圖片上傳實(shí)現(xiàn)即時(shí)刷新效果
- PHP 圖片文件上傳實(shí)現(xiàn)代碼
- 一個(gè)經(jīng)典的PHP文件上傳類分享
- PHP文件上傳實(shí)例詳解?。?!
- php+iframe 實(shí)現(xiàn)上傳文件功能示例
相關(guān)文章
php字符編碼轉(zhuǎn)換之gb2312轉(zhuǎn)為utf8
PHP輸出頁面時(shí)經(jīng)常有亂碼,怎么辦呢?今天我就提供一個(gè)方法,可以自動(dòng)判斷php字符編碼,把gbk或gb2312編碼的字符串轉(zhuǎn)為utf8 。2013-10-10Laravel 自動(dòng)轉(zhuǎn)換長(zhǎng)整型雪花 ID 為字符串的實(shí)現(xiàn)
這篇文章主要介紹了Laravel 自動(dòng)轉(zhuǎn)換長(zhǎng)整型雪花 ID 為字符串的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10PHP獲取遠(yuǎn)程http或ftp文件的md5值的方法
這篇文章主要介紹了PHP獲取遠(yuǎn)程http或ftp文件的md5值 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04php實(shí)現(xiàn)統(tǒng)計(jì)IP數(shù)及在線人數(shù)的示例代碼
這篇文章主要介紹了php實(shí)現(xiàn)統(tǒng)計(jì)IP數(shù)及在線人數(shù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07PHP隨機(jī)獲取未被微信屏蔽的域名(微信域名檢測(cè))
這篇文章主要介紹了PHP隨機(jī)獲取未被微信屏蔽的域名(微信域名檢測(cè)),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03