上传体系

phpwind里面不同地方的上传所调用的类是不一样的,这些类都位于lib\upload目录下面,都可以通过L::loadClass方式加载。而这些类都是继承lib\upload.class.php里面的uploadBehavior,这个类里面定义了很多常用的方法,其中有一部分是抽象方法,在使用时需要再被继承的类里面实现。文件的保存目录默认是在attachment目录下的,这个后台可以设置。下面是lib\upload这个目录下的文件类及其作用。

activeupload.class.php                                       活动相关附件上传类

activityupload.class.php                                    活动相关附件上传类

advupload.class.php                                          广告图片上传类

attmodify.class.php                                           附件编辑相关类

attupload.class.php                                           普通附件上传类

certificateupload.class.php                                 认证相关图片上传类

csvupload.class.php                                                 上传csv文件到服务器端相关

diaryupload.class.php                                        日志附件上传类

faceupload.class.php                                         头像上传类

messageupload.class.php                                   消息附件上传类

modifyattach.class.php                                      修改附件相关类

mutiupload.class.php                                        批量上传类

pcupload.class.php                                            分类信息附件上传类

photoupload.class.php                                       相册图片上传类

pushupload.class.php                                        推送数据图片上传类

stopicupload.class.php                                       专题图片上传类

 

上面这些类的使用方法都是大同小异的,比如要调用专题图片上传类,那么只需要下面这样写就行了

    L::loadClass(advupload, 'upload', false);

    $att = array($_FILES['att']['name']);

    $my_att = new AdvUpload($aid); 

    PwUpload::upload($my_att); 

 

下面简单介绍一下如何自己写一个上传的类供自己使用。

3、首先在lib\upload目录下创建一个类文件,myupload.class.php

myupload.class.php中继承uploadBehavior方法,并且实现一些自己需要用到的方法,例如allowTypegetFilePathupdate等等,代码如下
<?php

!defined('P_W') && exit('Forbidden');

//加载upload.class.php

L::loadClass('upload', '', false);

 

class MyUpload extends uploadBehavior {

 

   

    //初始化类

    function MyUpload() {

      

       parent::uploadBehavior();      

       //允许上传的文件大小上限

       $maxfilesize = 1000;

      

       $this->ftype = array(

           'gif'  => $maxfilesize,            'jpg'  => $maxfilesize,

           'jpeg' => $maxfilesize,            'txt'  => $maxfilesize,

           'png'  => $maxfilesize

       );

    }

    //表单file名称

    function allowType($key) {

       return true;

    }

 

 

    //获取文件路劲

    function getFilePath($currUpload) {

       global $timestamp;

       //这里主要就是设置一下保存的文件名规则

       $prename   = randstr(4) . $timestamp . substr(md5($timestamp . $currUpload['id'] . randstr(8)),10,15);

       $filename  = $prename .'.'. $currUpload['ext'];

       $savedir = 'myupload/';    

       return array($filename, $savedir);

    }

    //这边可以进行数据库更新操作等

    function update($uploaddb) {      

       return $uploaddb;

    }

 

    function getAttachs() {

       return $this->attachs;

    }

 

}

?>

2、在页面中调用自己写的这个类。调用方式如下test.php
<?php

require_once ('global.php');

require_once (R_P . 'require/functions.php');

S::gp(array(

    'action'

));

if($_POST&&$_POST['action'] == 'upload'){

    L::loadClass('myupload', 'upload', false);

    $att = array($_FILES['att']['name']);

    $my_att = new MyUpload($aid);

   

   

    var_dump(PwUpload::upload($my_att));

    }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gbk" />

<title>phpwind - powered by phpwind</title>

</head>

<body>

 <form method="post" enctype="multipart/form-data" action ="test.php">

<input type="hidden" name="action" value="upload" /><br/>

<input type="file" name="att" size="40"  /><br/>

<input type="submit"  value=' '>  

</form>

</body></html>