发布于 2014-10-30 13:58:07 | 195 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ThinkPHP开源PHP框架

ThinkPHP是一个开源的PHP框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP可以支持windows/Unix/Liunx等服务器环境,正式版需要PHP5.0以上版本支持,支持MySql、PgSQL、Sqlite以及PDO等多种数据库


本文是一个ThinkPHP结合AjaxFileUploader实现的无刷新文件上传的示例代码,是ThinkPHP文件上传中一个比较典型的应用技巧,感兴趣的同学参考下。

首先,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文件上传类就可以了,有一些插件我们需要到官方下载。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务