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)
{
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);
}
}
?>