function transByte($size) {
$arr = array ("B", "KB", "MB", "GB", "TB", "EB" );
while ( $size >= 1024 ) {
return round ( $size, 2 ) . $arr [$i];
* @param string $filename
function createFile($filename) {
//验证文件名的合法性,是否包含/,*,<>,?,|
$pattern = "/[\/,\*,<>,\?\|]/";
if (! preg_match ( $pattern, basename ( $filename ) )) {
if (! file_exists ( $filename )) {
if (touch ( $filename )) {
function renameFile($oldname,$newname){
// echo $oldname,$newname;
if(checkFilename($newname)){
if(!file_exists($path."/".$newname)){
if(rename($oldname,$path."/".$newname)){
* @param string $filename
function checkFilename($filename){
$pattern = "/[\/,\*,<>,\?\|]/";
if (preg_match ( $pattern, $filename )) {
* @param string $filename
function delFile($filename){
* @param string $filename
function downFile($filename){
header("content-disposition:attachment;filename=".basename($filename));
header("content-length:".filesize($filename));
* @param string $filename
function copyFile($filename,$dstname){
if(file_exists($dstname)){
if(!file_exists($dstname."/".basename($filename))){
if(copy($filename,$dstname."/".basename($filename))){
function cutFile($filename,$dstname){
if(file_exists($dstname)){
if(!file_exists($dstname."/".basename($filename))){
if(rename($filename,$dstname."/".basename($filename))){
function uploadFile($fileInfo,$path,$allowExt=array("gif","jpeg","jpg","png","txt"),$maxSize=10485760){
if($fileInfo['error']==UPLOAD_ERR_OK){
//文件是否是通过HTTP POST方式上传上来的
if(is_uploaded_file($fileInfo['tmp_name'])){
//上传文件的文件名,只允许上传jpeg|jpg、png、gif、txt的文件
//$allowExt=array("gif","jpeg","jpg","png","txt");
$ext=getExt($fileInfo['name']);
$destination=$path."/".pathinfo($fileInfo['name'],PATHINFO_FILENAME)."_".$uniqid.".".$ext;
if(in_array($ext,$allowExt)){
if($fileInfo['size']<=$maxSize){
if(move_uploaded_file($fileInfo['tmp_name'], $destination)){
$mes="文件不是通过HTTP POST方式上传上来的";
switch($fileInfo['error']){