欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ThinkPHP结合AjaxFileUploader实现无刷新文件上传的方法

程序员文章站 2022-05-31 12:35:42
本文实例讲述了thinkphp结合ajaxfileuploader实现无刷新文件上传的方法。分享给大家供大家参考。具体实现方法分析如下: 首先,ajaxfileuploa...

本文实例讲述了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程序设计有所帮助。