最近新写的几个库类文件,给大家PP吧。
时间:2006-06-10
来源:互联网
mvc框架下一步发出来。
注意:1、所有程序在php5+win xp下通过测试
2、所有的exception类可以改称Exception。(因为都只是简单的继承与exception,主要是方便扩展。熟悉zend framework的应该很熟悉的^_^)
参考phplib修改的,测试效率很好,高于原来phplib的数据库连接类
PHP代码:
<?php
/**
* filename: DB_Mysql.class.php
* @package:phpbean
* @author :feifengxlq<[email][email protected][/email]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.1
* create:2006-5-30
* description:the interface of mysql.
*
* example:
* ////////////Select action (First mode)//////////////////////////////
try{
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->query("select * from test");
for($i=0;$i<$mysql->num_rows($rs);$i++)
$record[$i]=$mysql->seek($i);
print_r($record);
$mysql->close();
}catch (Exception $e)
{
printf("%s",$e->__toString());
}
* ////////////Select action (Second mode)//////////////////////////////
try{
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->execute("select * from test");
print_r($rs);
$mysql->close();
}catch (Exception $e)
{
printf("%s",$e->__toString());
}
* /////////////insert action////////////////////////////
try {
$mysql=new DB_Mysql("localhost","root","root","root");
$mysql->query("insert into test(username) values('test from my DB_mysql')");
printf("%s",$mysql->insert_id());
$mysql->close();
}catch (Exception $e)
{
printf("%s",$e->__toString());
}
*/
require_once(ROOT_PATH."/../library/libs/exception/DB_Mysql_Exception.class.php");
class DB_Mysql{
/* private: connection parameters */
private $host="localhost";
private $database="";
private $user="root";
private $password="";
/* private: configuration parameters */
private $pconnect=false;
private $debug=false;
/* private: result array and current row number */
private $link_id=0;
private $query_id=0;
private $record=array();
/**
* construct
*
* @param string $host
* @param string $user
* @param string $password
* @param string $database
*/
public function __construct($host="localhost",$user="root",$password="",$database="")
{
$this->set("host",$host);
$this->set("user",$user);
$this->set("password",$password);
$this->set("database",$database);
$this->connect();
}
/**
* set the value for the param of this class
*
* @param string $var
* @param string $value
*/
public function set($var,$value)
{
$this->$var=$value;
}
/**
* connect to a mysql server,and choose the database.
*
* @param string $database
* @param string $host
* @param string $user
* @param string $password
* @return link_id
*/
public function connect($database="",$host="",$user="",$password="")
{
if(!empty($database))$this->set("database",$database);
if(!empty($host))$this->set("host",$host);
if(!empty($user))$this->set("user",$user);
if(!empty($password))$this->set("password",$password);
if($this->link_id==0)
{
if($this->pconnect)
$this->link_id=@mysql_pconnect($this->host,$this->user,$this->password);
else
$this->link_id=@mysql_connect($this->host,$this->user,$this->password);
if(!$this->link_id)
throw new DB_Mysql_Exception("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
if(!@mysql_select_db($this->database,$this->link_id))
throw new DB_Mysql_Exception("Mysql Select database Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
}
return $this->link_id;
}
/**
* query a sql into the database
*
* @param string $strsql
* @return query_id
*/
public function query($strsql="")
{
if(empty($strsql)) throw new DB_Mysql_Exception("Mysql Error:".__FUNCTION__."() strsql is empty!");
if($this->link_id==0) $this->connect();
if($this->debug) printf("Debug query sql:%s",$strsql);
$this->query_id=@mysql_query($strsql,$this->link_id);
if(!$this->query_id) throw new DB_Mysql_Exception("Mysql query fail,Invalid sql:".$strsql.".");
return $this->query_id;
}
/**
* query a sql into the database,while it is differernt from the query() method,
* this method will return a record(array);
*
* @param string $strsql
* @param string $style
* @return $record is a array()
*/
public function Execute($strsql,$style="array")
{
$this->query($strsql);
if(!empty($this->record))$this->record=array();
$i=0;
if($style=="array"){
while ($temp=@mysql_fetch_array($this->query_id)) {
$this->record[$i]=$temp;
$i++;
}
}else{
while ($temp=@mysql_fetch_object($this->query_id)) {
$this->record[$i]=$temp;
$i++;
}
}
unset($i);
unset($temp);
return $this->record;
}
/**
* seek,but not equal to mysql_data_seek. this methord will return a list.
*
* @param int $pos
* @param string $style
* @return record
*/
public function seek($pos=0,$style="array")
{
if(!@mysql_data_seek($this->query_id,$pos))
throw new DB_Mysql_Exception("Error in".__FUNCTION__."():can not seek to row ".$pos."!");
$result=@($style=="array")?mysql_fetch_array($this->query_id):mysql_fetch_object($this->query_id);
if(!$result) throw new DB_Mysql_Exception("Error in ".__FUNCTION__."():can not fetch data!");
return $result;
}
/**
* free the result of query
*
*/
public function free()
{
if(($this->query_id)&($this->query_id!=0))@mysql_free_result($this->query_id);
}
/**
* evaluate the result (size, width)
*
* @return num
*/
public function affected_rows()
{
return @mysql_affected_rows($this->link_id);
}
public function num_rows()
{
return @mysql_num_rows($this->query_id);
}
public function num_fields()
{
return @mysql_num_fields($this->query_id);
}
public function insert_id()
{
return @mysql_insert_id($this->link_id);
}
public function close()
{
$this->free();
if($this->link_id!=0)@mysql_close($this->link_id);
if(mysql_errno()!=0) throw new DB_Mysql_Exception("Mysql Error:".mysql_errno().":".mysql_error());
}
public function select($strsql,$number,$offset)
{
if(empty($number)){
return $this->Execute($strsql);
}else{
return $this->Execute($strsql.' limit '.$offset.','.$number);
}
}
public function __destruct()
{
$this->close();
$this->set("user","");
$this->set("host","");
$this->set("password","");
$this->set("database","");
}
}
?>
作者: feifengxlq 发布时间: 2006-06-09
PHP代码:
<?
/**
* filename: mySmarty.class.php
* @package:phpbean
* @author :feifengxlq<[email][email protected][/email]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.1
* create:2006-3
* modify:2006-5-27
* description:the encapsulation of smarty
*/
require_once(ROOT_PATH."/../library/drivers/smarty/smarty.class.php");
require_once(ROOT_PATH."/../library/libs/exception/mySmarty_Exception.class.php");
class mySmarty extends Smarty{
private $html_dir;//store the template html files
/**
* action where the class "mySmarty" is created
*
* @param string $style
*/
public function __construct($style="default")
{
$this->set_dir("template_dir",ROOT_PATH."/../templates/".$style);
$this->set_dir("compile_dir",ROOT_PATH."/../templates/compile",true);
$this->set_dir("config_dir",ROOT_PATH."/../templates/conf");
$this->set_dir("cache_dir",ROOT_PATH."/../templates/cache",true);
$this->set_dir("html_dir",ROOT_PATH."/../templates/html",true);
$this->left_delimiter = '<{';
$this->right_delimiter = '}>';
}
/**
* set basic dirs for smarty
*
* @param string $var
* @param string $dir:fliepath
* @param boolean $is_write:default false
* @return unknown
*/
public function set_dir($var,$dir,$is_write=false)
{
if($is_write&&!(is_writable($dir)))
throw new mySmarty_Exception(__FUNCTION__." Error:".$var." is not writable!");
$this->$var=$dir;
}
/**
* create a html file
* notice:the server must support php 5.0if you want to use this function
*
* @param string $template_file:template file path
* @param String $target_file: the html file's name while will be created
* @return boolean,true if create this html file !
*/
public function html($template_file,$target_file)
{
if(!@file_put_contents($this->html_dir."/".$target_file,$this->fetch($template_file)))
throw new mySmarty_Exception(__FUNCTION__." Error: create '".$this->html_dir."/".$target_file."' false !");
return true;
}
}
?>
作者: feifengxlq 发布时间: 2006-06-09
PHP代码:
<?
/**
* filename: Mysql.class.php
* @package:phpbean
* @author :feifengxlq<[email][email protected][/email]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.1
* create:2006-3
* modify:2006-5-27
* description:the interface of mysql. use adodb .
*/
require_once(ROOT_PATH."/../library/drivers/adodb/adodb.inc.php");
require_once(ROOT_PATH."/../library/libs/exception/Mysql_Exception.class.php");
class mysql
{
private $server;
private $user;
private $password;
private $database;
private $adodb;
private $debug=false;// control the mode,default flase
/**
* construct ,action where this class is created.
*
* @param string $server
* @param string $user
* @param string $password
* @param string $database
*/
public function __Construct($server='localhost',$user='Root',$password='',$database='')
{
$this->server=$server;
$this->user=$user;
$this->password=$password;
$this->database=$database;
$this->adodb=$this->connect();
}
/**
* connect to mysql server
*
* @return $adodb;
*/
private function connect()
{
if ($this->server=="") throw new Mysql_Exception("Error in ".__FUNCTION__.": server is empty!");
$this->adodb=ADONewConnection("mysql");
$this->adodb->debug=$this->debug;
$this->adodb->connect($this->server,$this->user,$this->password,$this->database);
$this->adodb->SetFetchMode(ADODB_FETCH_ASSOC);
return $this->adodb;
}
/**
* query a sql into database just like mysql_query.
*
* @param string $strsql
* @param boolean $inputarr
* @return $rs the result of the sql query
*/
public function query($strsql,$inputarr=false)
{
$rs=$this->adodb->Execute($strsql);
if($rs) return $rs;
throw new Mysql_Exception(__FUNCTION__." encount a Error! Error message is '".$this->adodb->errormsg()."'");
}
/**
* select $number lists from database.
* notice:this method is different from the $this->query method,this method will return as an array
* while $this->query ethod will return a ADORecordSet_mysql.
* @param string $strsql
* @param int $number
* @param int $offset
* @return $rs
*/
public function select($strsql,$number=0,$offset=0)
{
if(empty($number))
$rs=$this->adodb->Execute($strsql);
else
$rs=$this->adodb->SelectLimit($strsql,$number,$offset);
if($rs) return $rs->GetRows();
throw new Mysql_Exception(__FUNCTION__." encount a Error! Error message is '".$this->adodb->errormsg()."'");
}
/**
* get $adodb if you need
*
* @return $adodb
*/
public function getadodb()
{
return $this->adodb;
}
/**
* set the use mode ,you can choose debug or not
*
* @param boolean $debug
* @return boolean :if failed return false,else return true
*/
public function setdebug($debug)
{
if(is_bool($debug))
{
$this->debug=$debug;
return true;
}else
throw new Mysql_Exception("Error in ".__FUNCTION__." :input '$debug' is not a boolean!");
return false;
}
/**
* get error message
*
* @return String
*/
public function getErrorMsg()
{
return $this->adodb->errormsg();
}
/**
* close this mysql connection
*
*/
public function close()
{
$this->adodb->close();
}
/**
* __destuct:close the mysql connection
*
*/
public function __destruct()
{
$this->close();
}
}
?>
作者: feifengxlq 发布时间: 2006-06-09
,效果请看
http://www.phpchina.cn/bbs/viewt ... 1%26filter%3Ddigest
PHP代码:
<?
/**
* filename: PB_Page.class.php
* @package:phpbean
* @author :feifengxlq<[email][email protected][/email]><[url]http://xlq521.blog.sohu.com/[/url]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.1
* create:2006-5-31
* modify:2006-6-1
* description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。
* 目前尚为测试版本,欢迎大家提取改进的建议。我的邮箱:[email][email protected][/email]
* example:
$total = 1000;
$onepage = 20;
$pb_page=new PB_Page($total,$onepage);
echo $pb_page->page(1);
echo "<br>offset:".$pb_page->offset();
*/
require_once(ROOT_PATH."/../library/libs/exception/PB_Page_Exception.class.php");
class PB_Page extends PB_object
{
/**
* config
*/
public $page_name="PB_page";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
public $perpage=10;//每页显示记录条数
public $pagebarnum=10;//控制记录条的个数。
public $total=0;//总的记录数
public $next_page='>';//下一页
public $pre_page='<';//上一页
public $first_page='First';//首页
public $last_page='Last';//尾页
public $pre_bar='<<';//上一分页条
public $next_bar='>>';//下一分页条
public $format_left='[';
public $format_right=']';
/**
* private
*
*/
private $totalpage=0;//总页数
private $linkhead="";//url地址头
private $current_pageno=1;//当前页
/**
* constructor构造函数
*
* @param int $total
* @param int $perpage
*/
public function __construct($total,$perpage=10)
{
if((!is_int($total))||($total<0))throw new PB_Page_Exception('Invalid data:$total is not a positive integer!');
if((!is_int($perpage))||($perpage<0))throw new PB_Page_Exception('Invalid data:$perpageis not a positive integer!');
$this->total=$total;
$this->perpage=$perpage;
$this->totalpage=ceil($total/$perpage);
}
/**
* 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
*
* @param string $var
* @param string $value
*/
public function set($var,$value)
{
if(in_array($var,get_object_vars($this)))
$this->$var=$value;
else {
throw new PB_Page_Exception("Error in set():".$var." does not belong to PB_Page!");
}
}
/**
* get the default url获取指定的url地址
*
*/
public function get_linkhead()
{
if(empty($_SERVER['QUERY_STRING']))
$this->linkhead=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
else{
if(isset($_GET[$this->page_name])){
$this->linkhead=str_replace($this->page_name.'='.$this->current_pageno,$this->page_name.'=',$_SERVER['REQUEST_URI']);
}else {
$this->linkhead=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
}
}
}
/**
* 为指定的页面返回地址值
*
* @param int $pageno
* @return string $url
*/
public function get_url($pageno=1)
{
return $this->linkhead.$pageno;
return str_replace($this->page_name.'=',$this->page_name.'='.$pageno,$this->linkhead);
}
/**
* 设置当前页面
*
*/
public function set_current_page($current_pageno=0)
{
if(empty($current_pageno)){
if(isset($_GET[$this->page_name])){
$this->current_pageno=intval($_GET[$this->page_name]);
}
}else{
$this->current_pageno=intval($current_pageno);
}
}
public function set_format($str)
{
return $this->format_left.$str.$this->format_right;
}
/**
* 获取显示"下一页"的代码
*
* @return string
*/
public function next_page()
{
if($this->current_pageno<$this->totalpage){
return '<a href="'.$this->get_url($this->current_pageno+1).'">'.$this->next_page.'</a>';
}
return '';
}
/**
* 获取显示“上一页”的代码
*
* @return string
*/
public function pre_page()
{
if($this->current_pageno>1){
return '<a href="'.$this->get_url($this->current_pageno-1).'">'.$this->pre_page.'</a>';
}
return '';
}
/**
* 获取显示“首页”的代码
*
* @return string
*/
public function first_page()
{
return '<a href="'.$this->get_url(1).'">'.$this->first_page."</a>";
}
/**
* 获取显示“尾页”的代码
*
* @return string
*/
public function last_page()
{
return '<a href="'.$this->get_url($this->totalpage).'">'.$this->last_page.'</a>';
}
public function nowbar()
{
$begin=$this->current_pageno-ceil($this->pagebarnum/2);
$begin=($begin>=1)?$begin:1;
$return='';
for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
{
if($i<=$this->totalpage){
if($i!=$this->current_pageno)
$return.=$this->set_format('<a href="'.$this->get_url($i).'">'.$i.'</a>');
else
$return.=$this->set_format($i);
}else{
break;
}
}
unset($begin);
return $return;
}
/**
* 获取显示“上一分页条”的代码
*
* @return string
*/
public function pre_bar()
{
if($this->current_pageno>ceil($this->pagebarnum/2)){
$pageno=$this->current_pageno-$this->pagebarnum;
if($pageno<=0)$pageno=1;
return $this->set_format('<a href="'.$this->get_url($pageno).'">'.$this->pre_bar."</a>");
}
return $this->set_format('<a href="'.$this->get_url(1).'">'.$this->pre_bar."</a>");
}
/**
* 获取显示“下一分页条”的代码
*
* @return string
*/
public function next_bar()
{
if($this->current_pageno<$this->totalpage-ceil($this->pagebarnum/2)){
$pageno=$this->current_pageno+$this->pagebarnum;
if($pageno>$this->totalpage)$pageno=$this->totalpage;
return $this->set_format('<a href="'.$this->get_url($pageno).'">'.$this->next_bar."</a>");
}
return $this->set_format('<a href="'.$this->get_url($this->totalpage).'">'.$this->next_bar."</a>");
}
/**
* 获取显示跳转按钮的代码
*
* @return string
*/
public function select()
{
$return='<select name="PB_Page_Select" onchange="window.location.href=\''.$this->linkhead.'\'+this.options[this.selectedIndex].value">';
for($i=1;$i<=$this->totalpage;$i++)
{
if($i==$this->current_pageno){
$return.='<option value="'.$i.'" selected>'.$i.'</option>';
}else{
$return.='<option value="'.$i.'">'.$i.'</option>';
}
}
unset($i);
$return.='</select>';
return $return;
}
/**
* 获取mysql 语句中limit需要的值
*
* @return string
*/
public function offset()
{
return ($this->current_pageno-1)*$this->perpage;
}
/**
* 控制分页显示风格(你可以增加相应的风格)
*
* @param int $mode
* @return string
*/
public function page($mode=1)
{
$this->set_current_page();
$this->get_linkhead();
switch ($mode)
{
case '1':
$this->next_page='下一页';
$this->pre_page='上一页';
return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
break;
case '2':
$this->next_page='下一页';
$this->pre_page='上一页';
$this->first_page='首页';
$this->last_page='尾页';
return $this->first_page().$this->pre_page().'[第'.$this->current_pageno.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
break;
case '3':
$this->next_page='下一页';
$this->pre_page='上一页';
$this->first_page='首页';
$this->last_page='尾页';
return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
break;
case '4':
return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
break;
}
}
}
?>
作者: feifengxlq 发布时间: 2006-06-09
PHP代码:
<?php
/**
* filename: PB_upload.class.php
* @package:phpbean
* @author :feifengxlq<[email][email protected][/email]><[url]http://xlq521.blog.sohu.com/[/url]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.1
* create:2006-6-1
* description:文件上传类。支持不同形式的多文件上传
* 支持两种不同形式的多文件上传
* 形式1:
* <input name="filename[]" type="file">
<input name="filename[]" type="file">
* 形式2:
* <input name="filename1" type="file">
<input name="filename2" type="file">
* 目前尚为测试版本,欢迎大家提取改进的建议。我的邮箱:[email][email protected][/email]
* example:
* try{
$upload=new PB_upload($uploaddir,'filename','ini,doc');
if($upload->upload())echo 'succ<br>';
}catch (Exception $e){
print $e->__toString();
}
*/
require_once(ROOT_PATH.'/../library/libs/exception/PB_upload_Exception.class.php');
class PB_upload extends PB_object
{
public $upload_path='uploadfiles/';//上传文件的路径
public $allow_type=array();//允许上传的文件类型
public $max_size='2048';//允许的最大文件大小
public $file_field='uploadfile';//上传文件的字段名,可以为数组
public $overwrite=false;//是否设置成覆盖模式
public $renamed=false;//是否直接使用上传文件的名称,还是系统自动命名
/**
* 私有变量
*/
private $upload_file=array();//保存上传成功文件的信息
private $upload_file_num=0;//上传成功文件的数目
/**
* 构造器
*
* @param string $upload_path
* @param string $file_field
* @param string $allow_type
* @param string $max_size
*/
public function __construct($upload_path='uploadfiles/',$file_field='uploadfile',$allow_type='jpg,bmp,png,gif,jpeg',$max_size='2048')
{
$this->set_upload_path($upload_path);
$this->set_allow_type($allow_type);
$this->max_size=$max_size;
$this->file_field=$file_field;
$this->get_upload_files();
}
/**
* 设置上传路径,并判定
*
* @param string $path
*/
public function set_upload_path($path)
{
if(file_exists($path)){
if(is_writeable($path)){
$this->upload_path=$path;
}else{
if(@chmod($path,'0666'))
$this->upload_path=$path;
else
throw new PB_upload_Exception(''.__FUNCTION__.'()出错:修改权限出错!');
}
}else{
if(@mkdir($path,'0666')){
$this->upload_path=$path;
}else{
throw new PB_upload_Exception(''.__FUNCTION__.'()出错:建目录出错,请手工创建!');
}
}
}
/**
* 设置允许的上传文件类型
*
* @param string $type
*/
public function set_allow_type($type)
{
include_once('PB_filetype.class.php');
if(strtolower($type)=='all'){
$this->allow_type=$filetype;
}else{
$temp=array_unique(explode(',',strtolower($type)));
for($i=0;$i<count($temp);$i++){
if($filetype[$temp[$i]])
$this->allow_type[$temp[$i]]=$filetype[$temp[$i]];
else
throw new PB_upload_Exception(''.__FUNCTION__.'()出错:'.$temp[$i].' 系统不能识别!');
}
}
}
/**
* 格式化文件大小
*
* @param string $size
* @return unknown
*/
public function format_size($size)
{
if($size<1024){
return $size.'B';
}elseif ($size<1024*1024){
return number_format((double)($size/1024),2).'KB';
}else{
return number_format((double)($size/(1024*1024)),2).'MB';
}
}
/**
* 为系统属性赋值,主要用来修改配置信息
*
* @param string $var
* @param string $value
*/
public function set($var,$value)
{
if(!in_array($var,get_class_vars($this)))throw new PB_upload_Exception(''.__FUNCTION__.'()出错:'.$var.' 不是该对象的属性!');
$this->$var=$value;
}
/**
* 获取上传文件的信息,支持多文件上传
*
*/
public function get_upload_files()
{
if(is_array($this->file_field)){
foreach ($this->file_field as $key=>$field){
$this->get_upload_files_detial($field);
}
}else{
$this->get_upload_files_detial($this->file_field);
}
}
/**
* 详细获取上传文件信息
*
* @param string $field
*/
public function get_upload_files_detial($field){
if(is_array($_FILES[$field]['name'])){
for($i=0;$i<count($_FILES[$field]['name']);$i++)
{
if(0==$_FILES[$field]['error'][$i])
{
$this->upload_file[$this->upload_file_num]['name']=$_FILES[$field]['name'][$i];
$this->upload_file[$this->upload_file_num]['type']=$_FILES[$field]['type'][$i];
$this->upload_file[$this->upload_file_num]['size']=$_FILES[$field]['size'][$i];
$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES[$field]['tmp_name'][$i];
$this->upload_file[$this->upload_file_num]['error']=$_FILES[$field]['error'][$i];
$this->upload_file_num++;
}else
throw new PB_upload_Exception($this->error($_FILES[$field]['error'][$i]));
}
}else{
if(0==$_FILES[$field]['error'][$i])
{
$this->upload_file[$this->upload_file_num]['name']=$_FILES[$field]['name'];
$this->upload_file[$this->upload_file_num]['type']=$_FILES[$field]['type'];
$this->upload_file[$this->upload_file_num]['size']=$_FILES[$field]['size'];
$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES[$field]['tmp_name'];
$this->upload_file[$this->upload_file_num]['error']=$_FILES[$field]['error'];
$this->upload_file_num++;
}else
throw new PB_upload_Exception($this->error($_FILES[$field]['error'][$i]));
}
}
/**
* 检查上传文件是构满足指定条件
*
*/
public function check()
{
$check_msg='';
for($i=0;$i<$this->upload_file_num;$i++){
if(!empty($this->upload_file[$i]['name'])){
//检查文件大小
if($this->upload_file[$i]['size']>$this->max_size*1024)$check_msg.='文件'.$this->upload_file[$i]['name'].' 大小为 '.$this->upload_file[$i]['size'].'超过指定大小<br>';
//检查文件类型
if(!in_array($this->upload_file[$i][type],$this->allow_type))$check_msg.='文件:'.$this->upload_file[$i]['name'].'的类型不符合<br>';
//设置默认服务端文件名
$this->upload_file[$i]['filename']=$this->upload_path.$this->upload_file[$i]['name'];
//获取文件路径信息
$file_info=pathinfo($this->upload_file[$i]['name']);
//获取文件扩展名
$file_ext=$file_info['extension'];
//需要重命名的
if($this->renamed){
list($usec, $sec) = explode(" ",microtime());
$this->upload_file[$i]['filename']=$sec.substr($usec,2).'.'.$file_ext;
unset($usec);
unset($sec);
}
//检查文件是否存在
if(file_exists($this->upload_file[$i]['filename'])){
if($this->overwrite){
@unlink($this->upload_file[$i]['filename']);
}else{
$j=0;
do{
$j++;
$temp_file=str_replace('.'.$file_ext,'('.$j.').'.$file_ext,$this->upload_file[$i]['filename']);
}while (file_exists($temp_file));
$this->upload_file[$i]['filename']=$temp_file;
unset($temp_file);
unset($j);
}
}
}
//检查完毕
}
if(!empty($check_msg)) throw new PB_upload_Exception('上传文件失败:'.__FUNCTION__.'()出错信息:'.$check_msg);
}
/**
* 上传文件
*
* @return true
*/
public function upload()
{
$this->check();
$upload_msg='';
for($i=0;$i<$this->upload_file_num;$i++)
{
if(!empty($this->upload_file[$i]['name']))
{
//上传文件
if(!@move_uploaded_file($this->upload_file[$i]['tmp_name'],$this->upload_file[$i]['filename']))
$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!<br>';
}
}
if(!empty($upload_msg))throw new PB_upload_Exception('上传失败:'.$upload_msg);
return true;
}
/**
* 格式化出错处理
*
* @param int $error
* @return string
*/
public function error($error)
{
switch ($error) {
case 1:
return '文件大小超过php.ini 中 upload_max_filesize 选项限制的值';
break;
case 2:
return '文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
return '文件只有部分被上传';
break;
case 4:
return '没有文件被上传';
break;
default:
break;
}
}
/**
* 获取上传文件信息
*
* @return 二维数组
*/
public function get_file(){
return $this->upload_file;
}
}
?>
作者: feifengxlq 发布时间: 2006-06-09
不过有个建议~~
采用__get和__set方法来取代get和set方法~~~这样可以在重构代码时获得更多的便利~~
作者: mikespook 发布时间: 2006-06-09
作者: heiyeluren 发布时间: 2006-06-11
作者: alex 发布时间: 2006-06-12
作者: forest 发布时间: 2006-06-12
作者: szy_session1987 发布时间: 2006-06-13
作者: fnet 发布时间: 2006-06-14
作者: DODOphp 发布时间: 2007-08-20
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28