程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> ThinkPHP結合AjaxFileUploader實現無刷新文件上傳的方法

ThinkPHP結合AjaxFileUploader實現無刷新文件上傳的方法

編輯:PHP綜合

本文實例講述了ThinkPHP結合AjaxFileUploader實現無刷新文件上傳的方法。分享給大家供大家參考。具體實現方法分析如下:

首先,AjaxFileUploader插件是一個基於jquery的插件,我們可以使用AjaxFileUploader插件來實現文件異步上傳功能了,使用這款插件上傳文件不要擔心兼容性的問題,它的兼容性可以說兼容所有主流浏覽器,下面來給大家介紹一個AjaxFileUploader+thinkphp實現文件上傳的實例。

ThinkPHP框架下用AjaxFileUploader插件實現ajax文件上傳,支持多種文件格式,頁面無刷新上傳。

在Lib/Action/目錄下創建upAction.class.php文件,代碼如下:

復制代碼 代碼如下:<?php
class upAction extends BaseAction{
public function index(){
    $this->display();
}
 
/*
*@文件上傳
*@author    FineYi
*@date        2013-01-23
*/
public function upLoadFile(){
    $error = "";
    $msg = "";
    $fileElementName = 'fileToUpload';
    if(!empty($_FILES[$fileElementName]['error'])){
        switch($_FILES[$fileElementName]['error']){
            case '1':
                $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                break;
            case '2':
                $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                break;
            case '3':
                $error = 'The uploaded file was only partially uploaded';
                break;
            case '4':
                $error = 'No file was uploaded.';
                break;
 
            case '6':
                $error = 'Missing a temporary folder';
                break;
            case '7':
                $error = 'Failed to write file to disk';
                break;
            case '8':
                $error = 'File upload stopped by extension';
                break;
            case '999':
            default:
                $error = 'No error code avaiable';
        }
    }elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none'){
        $error = 'No file was uploaded..';
    }else{
            $re = $this->up();
            if(!$re){
                $error = 'Up file fail';
            }
            $msg = $re['savename'];    //文件名
            $path = '/upload/bizcoop/'.$msg;    //文件路徑
            $size = $re['size'];    //文件大小
    }       
    echo json_encode(array('error'=>$error,'msg'=>$msg,'path'=>$path,'size'=>$size));exit;
}
 
private function up(){
    import('@.Org.UploadFile');//將上傳類UploadFile.class.php拷到Lib/Org文件夾下
    $upload=new UploadFile();
 
    $upload->maxSize='-1';//默認為-1,不限制上傳大小
    $upload->savePath= ICTSPACE_DIST_ROOT_PATH.'/www/upload/bizcoop/';//保存路徑
    $upload->saveRule=uniqid;//上傳文件的文件名保存規則
    $upload->uploadReplace=true;//如果存在同名文件是否進行覆蓋
    $upload->allowExts=array('jpg','jpeg','png','gif');//准許上傳的文件類型
    if($upload->upload()){
        $info=$upload->getUploadFileInfo();
        return $info[0];
    }else{
        return false;
        exit;
    }
}
}
?>
在/Tpl/default/Up/目錄下創建index.tpl文件,代碼如下:

復制代碼 代碼如下:<div id="content">
<h1>Ajax File Upload Demo</h1>
<img id="loading" style="display: none;" alt="" src="__APP____PUBLIC__/style/img/loading.gif" />
 
<form action="" enctype="multipart/form-data" method="POST" name="form">
<table class="tableForm" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="input" id="fileToUpload" type="file" name="fileToUpload" size="45" /></td>
</tr>
<tr>
<td><button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button></td>
</tr>
</tbody>
<tbody>
<tr>
<td><span>已上傳的附件:</span></td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
</form></div>

在/Lib/Org/目錄下放入ThinkPHP文件上傳類就可以了,有一些插件我們需要到官方下載。

希望本文所述對大家的PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved