PHP工具代码

PHP工具代码

1. php文件上传错误检测

function check_error($file)
{
if ($file['error'] > 0) {
switch ($file['error'])
{
case 1:
exit('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
break;
case 2:
exit('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值');
break;
case 3:
exit('文件只有部分被上传');
break;
case 4:
exit('没有文件被上传');
break;
case 5:
exit('上传文件大小为0');
break;
}
}
}

2.PHP base64编码

function base64Code($str,$isEncode=false)
{ //true编码 false解码
return $isEncode ? rawurldecode(base64_decode($str)): rawurlencode(base64_encode($str));
}

3.PHP数据库简单包装类

<?php
/**
* 数据库管理类
*/
class DBManager
{
var $m_conn;
function __construct()
{
$this->m_conn = new mysqli('localhost','username','password','databaseName');
if ($this->m_conn->connect_error) {
$this->m_conn->close();
exit("数据库连接失败: " . $this->m_conn->connect_error);
}
}
//查询
function select($tab, $cols='*',$where='1',$ext='')
{
$sql = "select {$cols} from {$tab} where {$where} {$ext}";
$result = $this->m_conn->query($sql);
if(!$result) return $this->m_conn->error;
if ($result->num_rows > 0) {
$i = 0;
while($row = $result->fetch_assoc()) {
$data[$i] = $row;
$i++;
}
return $data;
}
else
return '';
}
//插入
function insert($tab, $vals, $cols='')
{
$sql = "insert into {$tab} ({$cols}) values({$vals})";
if(!$this->m_conn->query($sql))
return $this->m_conn->error;
}
//更新
function update($tab,$col,$val,$where='1')
{
$sql = "update {$tab} set {$col} = '{$val}' where {$where}";
if(!$this->m_conn->query($sql))
return $this->m_conn->error;
}
//删除
function delete($tab,$where)
{
$sql = "delete from {$tab} where {$where}";
if(!$this->m_conn->query($sql))
return $this->m_conn->error;
}
//清空表
function clear($tab)
{
$sql = "truncate table {$tab}";
if(!$this->m_conn->query($sql))
return $this->m_conn->error;
}
//联合查询
function join_select($user,$cols='*',$on='1', $start=0, $num =10000)
{
$sql = "SELECT {$cols} FROM `fileinfo` JOIN `userinfo` ON fileinfo.uid = userinfo.uid AND (fshare='1' OR uname='{$user}') AND {$on} LIMIT {$start},{$num};";
$result = $this->m_conn->query($sql);
if(!$result) return $this->m_conn->error;
if ($result->num_rows > 0) {
$i = 0;
while($row = $result->fetch_assoc()) {
$data[$i] = $row;
$i++;
}
return $data;
}
else
return '';
}
//数量查询
function count($tab,$where='1',$join = false)
{
if($join)
$res = $this->join_select('count(*)',$where);
else
$res = $this->select("{$tab}",'count(*)',$where);
return $res[0]['count(*)'];
}
//关键字查询
function keywordQuery($user,$kw,$cols='*',$start = 0,$num = 1000)
{
$on = "(fname LIKE '%{$kw}%' OR
fpath LIKE '%{$kw}%' OR
fdate LIKE '%{$kw}%' OR
foperation LIKE '%{$kw}%' OR
ftext LIKE '%{$kw}%' OR
fsuffix LIKE '%{$kw}%' OR
uname LIKE '%{$kw}%')";
return $this->join_select($user,$cols,$on,$start,$num);
}
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值