Edit File by line
/home/barbar84/public_h.../extracte.../dirfunc
File: file.func.php
<?php
[0] Fix | Delete
[1] Fix | Delete
//Bytes/Kb/MB/GB/TB/EB
[2] Fix | Delete
/**
[3] Fix | Delete
* 转换字节大小
[4] Fix | Delete
* @param number $size
[5] Fix | Delete
* @return number
[6] Fix | Delete
*/
[7] Fix | Delete
function transByte($size) {
[8] Fix | Delete
$arr = array ("B", "KB", "MB", "GB", "TB", "EB" );
[9] Fix | Delete
$i = 0;
[10] Fix | Delete
while ( $size >= 1024 ) {
[11] Fix | Delete
$size /= 1024;
[12] Fix | Delete
$i ++;
[13] Fix | Delete
}
[14] Fix | Delete
return round ( $size, 2 ) . $arr [$i];
[15] Fix | Delete
}
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* 创建文件
[19] Fix | Delete
* @param string $filename
[20] Fix | Delete
* @return string
[21] Fix | Delete
*/
[22] Fix | Delete
function createFile($filename) {
[23] Fix | Delete
//file/1.txt
[24] Fix | Delete
//验证文件名的合法性,是否包含/,*,<>,?,|
[25] Fix | Delete
$pattern = "/[\/,\*,<>,\?\|]/";
[26] Fix | Delete
if (! preg_match ( $pattern, basename ( $filename ) )) {
[27] Fix | Delete
//检测当前目录下是否存在同名文件
[28] Fix | Delete
if (! file_exists ( $filename )) {
[29] Fix | Delete
//通过touch($filename)来创建
[30] Fix | Delete
if (touch ( $filename )) {
[31] Fix | Delete
return "文件创建成功";
[32] Fix | Delete
} else {
[33] Fix | Delete
return "文件创建失败";
[34] Fix | Delete
}
[35] Fix | Delete
} else {
[36] Fix | Delete
return "文件已存在,请重命名后创建";
[37] Fix | Delete
}
[38] Fix | Delete
} else {
[39] Fix | Delete
return "非法文件名";
[40] Fix | Delete
}
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* 重命名文件
[45] Fix | Delete
* @param string $oldname
[46] Fix | Delete
* @param string $newname
[47] Fix | Delete
* @return string
[48] Fix | Delete
*/
[49] Fix | Delete
function renameFile($oldname,$newname){
[50] Fix | Delete
// echo $oldname,$newname;
[51] Fix | Delete
//验证文件名是否合法
[52] Fix | Delete
if(checkFilename($newname)){
[53] Fix | Delete
//检测当前目录下是否存在同名文件
[54] Fix | Delete
$path=dirname($oldname);
[55] Fix | Delete
if(!file_exists($path."/".$newname)){
[56] Fix | Delete
//进行重命名
[57] Fix | Delete
if(rename($oldname,$path."/".$newname)){
[58] Fix | Delete
return "重命名成功";
[59] Fix | Delete
}else{
[60] Fix | Delete
return "重命名失败";
[61] Fix | Delete
}
[62] Fix | Delete
}else{
[63] Fix | Delete
return "存在同名文件,请重新命名";
[64] Fix | Delete
}
[65] Fix | Delete
}else{
[66] Fix | Delete
return "非法文件名";
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
*检测文件名是否合法
[73] Fix | Delete
* @param string $filename
[74] Fix | Delete
* @return boolean
[75] Fix | Delete
*/
[76] Fix | Delete
function checkFilename($filename){
[77] Fix | Delete
$pattern = "/[\/,\*,<>,\?\|]/";
[78] Fix | Delete
if (preg_match ( $pattern, $filename )) {
[79] Fix | Delete
return false;
[80] Fix | Delete
}else{
[81] Fix | Delete
return true;
[82] Fix | Delete
}
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* 删除文件
[87] Fix | Delete
* @param string $filename
[88] Fix | Delete
* @return string
[89] Fix | Delete
*/
[90] Fix | Delete
function delFile($filename){
[91] Fix | Delete
if(unlink($filename)){
[92] Fix | Delete
$mes="文件删除成功";
[93] Fix | Delete
}else{
[94] Fix | Delete
$mes="文件删除失败";
[95] Fix | Delete
}
[96] Fix | Delete
return $mes;
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* 下载文件操作
[101] Fix | Delete
* @param string $filename
[102] Fix | Delete
*/
[103] Fix | Delete
function downFile($filename){
[104] Fix | Delete
header("content-disposition:attachment;filename=".basename($filename));
[105] Fix | Delete
header("content-length:".filesize($filename));
[106] Fix | Delete
readfile($filename);
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* 复制文件
[111] Fix | Delete
* @param string $filename
[112] Fix | Delete
* @param string $dstname
[113] Fix | Delete
* @return string
[114] Fix | Delete
*/
[115] Fix | Delete
function copyFile($filename,$dstname){
[116] Fix | Delete
if(file_exists($dstname)){
[117] Fix | Delete
if(!file_exists($dstname."/".basename($filename))){
[118] Fix | Delete
if(copy($filename,$dstname."/".basename($filename))){
[119] Fix | Delete
$mes="文件复制成功";
[120] Fix | Delete
}else{
[121] Fix | Delete
$mes="文件复制失败";
[122] Fix | Delete
}
[123] Fix | Delete
}else{
[124] Fix | Delete
$mes="存在同名文件";
[125] Fix | Delete
}
[126] Fix | Delete
}else{
[127] Fix | Delete
$mes="目标目录不存在";
[128] Fix | Delete
}
[129] Fix | Delete
return $mes;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
function cutFile($filename,$dstname){
[133] Fix | Delete
if(file_exists($dstname)){
[134] Fix | Delete
if(!file_exists($dstname."/".basename($filename))){
[135] Fix | Delete
if(rename($filename,$dstname."/".basename($filename))){
[136] Fix | Delete
$mes="文件剪切成功";
[137] Fix | Delete
}else{
[138] Fix | Delete
$mes="文件剪切失败";
[139] Fix | Delete
}
[140] Fix | Delete
}else{
[141] Fix | Delete
$mes="存在同名文件";
[142] Fix | Delete
}
[143] Fix | Delete
}else{
[144] Fix | Delete
$mes="目标目录不存在";
[145] Fix | Delete
}
[146] Fix | Delete
return $mes;
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
/**
[150] Fix | Delete
* 上传文件
[151] Fix | Delete
* @param array $fileInfo
[152] Fix | Delete
* @param string $path
[153] Fix | Delete
* @param array $allowExt
[154] Fix | Delete
* @param int $maxSize
[155] Fix | Delete
* @return string
[156] Fix | Delete
*/
[157] Fix | Delete
function uploadFile($fileInfo,$path,$allowExt=array("gif","jpeg","jpg","png","txt"),$maxSize=10485760){
[158] Fix | Delete
//判断错误号
[159] Fix | Delete
if($fileInfo['error']==UPLOAD_ERR_OK){
[160] Fix | Delete
//文件是否是通过HTTP POST方式上传上来的
[161] Fix | Delete
if(is_uploaded_file($fileInfo['tmp_name'])){
[162] Fix | Delete
//上传文件的文件名,只允许上传jpeg|jpg、png、gif、txt的文件
[163] Fix | Delete
//$allowExt=array("gif","jpeg","jpg","png","txt");
[164] Fix | Delete
$ext=getExt($fileInfo['name']);
[165] Fix | Delete
$uniqid=getUniqidName();
[166] Fix | Delete
$destination=$path."/".pathinfo($fileInfo['name'],PATHINFO_FILENAME)."_".$uniqid.".".$ext;
[167] Fix | Delete
if(in_array($ext,$allowExt)){
[168] Fix | Delete
if($fileInfo['size']<=$maxSize){
[169] Fix | Delete
if(move_uploaded_file($fileInfo['tmp_name'], $destination)){
[170] Fix | Delete
$mes="文件上传成功";
[171] Fix | Delete
}else{
[172] Fix | Delete
$mes="文件移动失败";
[173] Fix | Delete
}
[174] Fix | Delete
}else{
[175] Fix | Delete
$mes="文件过大";
[176] Fix | Delete
}
[177] Fix | Delete
}else{
[178] Fix | Delete
$mes="非法文件类型";
[179] Fix | Delete
}
[180] Fix | Delete
}else{
[181] Fix | Delete
$mes="文件不是通过HTTP POST方式上传上来的";
[182] Fix | Delete
}
[183] Fix | Delete
}else{
[184] Fix | Delete
switch($fileInfo['error']){
[185] Fix | Delete
case 1:
[186] Fix | Delete
$mes="超过了配置文件的大小";
[187] Fix | Delete
break;
[188] Fix | Delete
case 2:
[189] Fix | Delete
$mes="超过了表单允许接收数据的大小";
[190] Fix | Delete
break;
[191] Fix | Delete
case 3:
[192] Fix | Delete
$mes="文件部分被上传";
[193] Fix | Delete
break;
[194] Fix | Delete
case 4:
[195] Fix | Delete
$mes="没有文件被上传";
[196] Fix | Delete
break;
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return $mes;
[201] Fix | Delete
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
[205] Fix | Delete
[206] Fix | Delete
[207] Fix | Delete
[208] Fix | Delete
[209] Fix | Delete
[210] Fix | Delete
[211] Fix | Delete
[212] Fix | Delete
[213] Fix | Delete
[214] Fix | Delete
[215] Fix | Delete
[216] Fix | Delete
[217] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function