發(fā)送一條sql語句,并且在sql語句錯(cuò)誤的時(shí)候輸出sql的錯(cuò)誤信息
$sql 要發(fā)送的sql語句
function query($sql){
// 發(fā)送一條查詢語句
$res = mysql_query($sql);
// 如果sql語句錯(cuò)誤,輸出錯(cuò)誤信息 并退出腳本 如果成功,返回結(jié)果集
if(!$res){
echo mysql_error().'<br />';
exit();
}
return $res;
}
獲取一個(gè)文件或者目錄的8進(jìn)制權(quán)限
function getFilePerms($fileName){
return substr(base_convert(fileperms($fileName),10,8), -4);
}
計(jì)算一個(gè)整數(shù)的階乘
function factorial($num){
if($num==1) return 1;
return $num*factorial($num-1);
}
統(tǒng)計(jì)出一個(gè)文件夾下面目錄的數(shù)量和文件的數(shù)量(引用傳值)
$dir 目錄名
function countFilesNumA($dir,&$dirNum=0,&$fileNum=0){
// 打開句柄
$handle = opendir($dir);
// 讀取. 和..
readdir($handle);
readdir($handle);
while($fileName = readdir($handle)){
// 拼接成路徑
$newFile = "$dir/$fileName";
// 如果是目錄遞歸
if(is_dir($newFile)){
$dirNum++;
countFilesNum($newFile,$dirNum,$fileNum);
}else{
$fileNum++;
}
}
// 關(guān)閉句柄
closedir($handle);
// 返回目錄數(shù)量和文件數(shù)量
return array($dirNum,$fileNum);
}
統(tǒng)計(jì)出一個(gè)文件夾下面目錄的數(shù)量和文件的數(shù)量
string $dir 目錄名
function countFilesNumB($dir){
// 打開句柄
$handle = opendir($dir);
// 讀取. 和..
readdir($handle);
readdir($handle);
$dirNum = 0;
$fileNum = 0;
while(($fileName = readdir($handle))||($fileName!==false)){
// 拼接成路徑
$newFile = "$dir/$fileName";
// 如果是目錄遞歸
if(is_dir($newFile)){
$dirNum++;
$res = countFilesNumB($newFile);
$dirNum += $res[0];
$fileNum += $res[1];
}else{
$fileNum++;
}
}
// 關(guān)閉句柄
closedir($handle);
// 返回目錄數(shù)量和文件數(shù)量
return array($dirNum,$fileNum);
}
** 刪除一個(gè)目錄**
$dir 目錄名
function delDir($dir){
// 打開句柄
$handle = opendir($dir);
// 讀取.和..
readdir($handle);
readdir($handle);
// 循環(huán)
while(($fileName = readdir($handle))||($fileName!==false)){
// 拼接路徑
$newFile = "$dir/$fileName";
if(is_dir($newFile)){
if(!delDir($newFile)){return false;}
}else{
unlink($newFile);
}
}
// 關(guān)閉句柄
closedir($handle);
// 刪除目錄
if(rmdir($dir)) return true;
return false;
}
實(shí)現(xiàn)一個(gè)文件的下載
$fileName 下載文件的路徑
function doDownload($fileName){
// 1、設(shè)置響應(yīng)頭為八進(jìn)制數(shù)據(jù)流
header('Content-type:application/octet-stream');
// 2、告訴瀏覽器傳送數(shù)據(jù)編碼方式為2進(jìn)制
header('Content-Transfer-Encoding: binary');
//3、支持?jǐn)帱c(diǎn)續(xù)傳 需要服務(wù)器支持?jǐn)帱c(diǎn)續(xù)傳功能
header('Accept-Ranges:bytes');
// 4、告訴客戶端文件大小
$filesize = filesize($fileName);
header("Accept-Length:$filesize");
// 5、告訴瀏覽器文件下載方式,以及下載的文件名
$tmpName = explode('/', $fileName);
$tmpName = array_pop($tmpName);
header("Content-Disposition:attachment;filename=$tmpName");
// 清空前面輸出
ob_end_clean();
//6、輸出文件流
$handle = fopen($fileName, 'rb');
while($con = fread($handle, 1024)){
echo $con;
}
}
生成一個(gè)3-6位的驗(yàn)證碼
function makeCode($len=4){
// 限制長度為3-6
$len = ($len>6) ? 6 : $len;
$len = ($len<3) ? 3 : $len;
// 創(chuàng)建真彩畫布
$img = imagecreatetruecolor(100, 40);
// 給畫布分配隨機(jī)背景顏色
$bgColor = imagecolorallocate($img, mt_rand(180,255), mt_rand(180,255), mt_rand(180,255));
// 給畫布填充背景顏色
imagefill($img, 0, 0, $bgColor);
// 字符庫
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
// 取出最大下標(biāo)
$maxIndex = strlen($str)-1;
// 定義驗(yàn)證碼初始值
$code = '';
$flag = 0;
while($flag<$len){
// 給文字隨機(jī)顏色
$color = imagecolorallocate($img, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
// 隨機(jī)取出一個(gè)字符
$tmpStr = $str{mt_rand(0,$maxIndex)};
$code .= $tmpStr;
$x = (105-$len*15)/2+$flag*15;
// 寫入字符串
imagestring($img, 5, $x, 10, $tmpStr, $color);
$flag++;
}
// 將驗(yàn)證碼存儲到session中
session_start();
$_SESSION['code'] = $code;
// 循環(huán)100個(gè)像素點(diǎn)
for($i=0; $i<100; $i++){
$color = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
imagesetpixel($img, mt_rand(0,100), mt_rand(0,40), $color);
}
// 輸出驗(yàn)證碼并釋放內(nèi)存
header('Content-type:image/jpeg');
// 清空前面輸出
ob_end_clean();
imagejpeg($img);
imagedestroy($img);
}
上傳一個(gè)文件
$file 里面包含五個(gè)元素
$fileMaxSize 上傳文件最大的大小
$extArray 允許的擴(kuò)展名數(shù)組
$mimeArray 允許的mime類型數(shù)組
$path 上傳文件保存的路徑
return array('upload_success'=>bool,'error_code'=>int,'error_info'=>'錯(cuò)誤信息','file_path'=>'上傳后文件保存的路徑')
function doUpload($file, $fileMaxSize, $extArray, $mimeArray, $path){
// 系統(tǒng)錯(cuò)誤
if($file['error'] != 0){
return changeCode($file['error']);
}
// 邏輯需要
// 判斷文件大小
if($file['size']>$fileMaxSize){
return changeCode(5);
}
// 判斷擴(kuò)展名是否符合規(guī)范
$ext = $file['name'];
$ext = explode('.', $ext);
$ext = array_pop($ext);
if(!in_array($ext, $extArray)){
return changeCode(8);
}
// 安全需要
// 取出文件的真實(shí)的mime類型
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $file['tmp_name']);
$mime = explode(';', $mime);
$mime = array_shift($mime);
if(!in_array($mime, $mimeArray)){
return changeCode(9);
}
// 拼接基于分鐘的文件夾
$dir = $path.'/'.date('Y-m-d-H-i');
// 目錄不存在生成目錄
if(!is_dir($dir)){
mkdir($dir,0777,true);
}
// 隨機(jī)永不重復(fù)名字
$tmpName = uniqid().$file['name'];
// 拼接成完整路徑
$destination = $dir.'/'.$tmpName;
// 移動(dòng)上傳文件
if(move_uploaded_file($file['tmp_name'], $destination)){
return array('upload_success'=>true,'error_code'=>0,'error_info'=>'上傳成功','file_path'=>$destination);
}
}
將錯(cuò)誤的編號轉(zhuǎn)為錯(cuò)誤的信息
$code 錯(cuò)誤編號
function changeCode($code){
switch($code){
// 上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值。
case 1: $error_info = '上傳文件過大';
break;
// 上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。
case 2: $error_info = '上傳文件過大';
break;
// 文件只有部分被上傳。
case 3: $error_info = '上傳錯(cuò)誤,請重新上傳';
break;
// 沒有文件被上傳。
case 4: $error_info = '上傳錯(cuò)誤,請重新上傳';
break;
// 找不到臨時(shí)文件夾。
case 6: $error_info = '上傳異常,請聯(lián)系管理員';
break;
// 文件寫入失敗。
case 7: $error_info = '上傳異常,請聯(lián)系管理員';
break;
// 超過了自定義的文件大小
case 5: $error_info = '上傳文件過大';
break;
// 擴(kuò)展名不正確
case 8: $error_info = '上傳文件類型不符合規(guī)范';
break;
// mime類型不正確
case 9: $error_info = '/(ㄒoㄒ)/~~';
break;
default;
}
return array('upload_success'=>false,'error_code'=>$code,'error_info'=>$error_info,'file_path'=>null);
}
實(shí)現(xiàn)圖像的等比縮放
$fileName 圖像的完整路徑
$scale 縮放比例 比如0.5表示等比縮放0.5倍
return string $newFile 生成的新的圖像的路徑
function geometricScaling($fileName,$scale=0.5){
$ext = array('', 'gif', 'jpeg', 'png');
// 獲取舊圖像的信息
$imgInfo = getimagesize($fileName);
// 拼接和圖像相對應(yīng)函數(shù)名
$funName = 'imagecreatefrom'.$ext[$imgInfo[2]];
// 舊圖像資源
$src_image = $funName($fileName);
// 舊圖像寬度
$src_w = $imgInfo[0];
// 舊圖像高度
$src_h = $imgInfo[1];
// 新圖像寬度
$dst_w = $src_w*$scale;
// 新圖像的高度
$dst_h = $src_h*$scale;
// 創(chuàng)建一個(gè)空畫布,用來容納裁剪后的圖像
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// 實(shí)現(xiàn)圖像等比縮放
imagecopyresampled($dst_image, $src_image,0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
// 拼接輸出函數(shù)名
$funName = 'image'.$ext[$imgInfo[2]];
// 生成一個(gè)新的地址
$newName = explode('/', $fileName);
// 求數(shù)組最大下標(biāo)
$maxIndex = count($newName)-1;
// 將文件名重新賦值
$newName[$maxIndex] = $scale.'_'.$newName[$maxIndex];
// 拼接成新的路徑
$newName = implode('/', $newName);
// 保存圖像
$funName($dst_image,$newName);
// 釋放內(nèi)存
imagedestroy($dst_image);
imagedestroy($src_image);
return $newName;
}