File: /web/data/www.tbbprovision.com/lib/class/file.class.php
<?php
class file{
public static function mk_dir($dir){ //建立上传目录
if($dir=='/' || is_dir(APP_PATH.$dir)){return $dir;}
$arr_dir=@explode('/', $dir);
for($i=0; $i<count($arr_dir); $i++){
$base_dir=APP_PATH;
for($j=0; $j<=$i; $j++){
$base_dir.=$arr_dir[$j].'/';
}
!is_dir($base_dir) && @mkdir($base_dir);
}
return $dir;
}
public static function get_ext_name($file=''){ //返回文件后辍名(小写)
return strtolower(pathinfo($file, PATHINFO_EXTENSION));
}
public static function get_base_name($file=''){ //返回文件名
return pathinfo($file, PATHINFO_BASENAME);
}
public static function file_upload($up_file_name, $save_dir){ //上传文件
file::mk_dir($save_dir);
if(substr_count(strtolower($up_file_name['name']), 'php') || substr_count(strtolower($up_file_name['name']), 'js')){
file::del_file($up_file_name['tmp_name']);
return '';
}else{
$ext_name=file::get_ext_name($up_file_name['name']);
$save_name=$save_dir.str::rand_code().'.'.$ext_name;
$save_path=APP_PATH.$save_name;
move_uploaded_file($up_file_name['tmp_name'], $save_path);
return is_file($save_path)?$save_name:'';
}
}
public static function file_upload_ext($up_file_name, $save_dir,$index=0){ //上传文件
file::mk_dir($save_dir);
if(substr_count(strtolower($up_file_name['name'][$index]), 'php')){
file::del_file($up_file_name['tmp_name'][$index]);
return '';
}else{
$ext_name=file::get_ext_name($up_file_name['name'][$index]);
$save_name=$save_dir.str::rand_code().'.'.$ext_name;
$save_path=APP_PATH.$save_name;
move_uploaded_file($up_file_name['tmp_name'][$index], $save_path);
return is_file($save_path)?$save_name:'';
}
}
public static function file_upload_swf($save_dir, $resize_ary='', $AddPhoto=true, $is_water=0){//is_bank是否返回图片银行的路径
$status=array('status'=>-1);
if($filepath=file::file_upload($_FILES['Filedata'], $save_dir)){
$water_ary=array();
if($is_water && $c['manage']['is_watermark']) $water_ary[]=$filepath;
if($resize_ary){
$size=$_POST['size'];
if(array_key_exists($size, $resize_ary)){
if(in_array('default', $resize_ary[$size])){//保存不加水印的原图
$ext_name=file::get_ext_name($filepath);
@copy(APP_PATH.$filepath, APP_PATH.$filepath.".default.{$ext_name}");
}
if($is_water && $c['manage']['is_watermark'] && $c['manage']['is_thumbnail']){//缩略图加水印
$new_file = img::img_add_watermark($filepath);
$water_ary=array();
}
foreach((array)$resize_ary[$size] as $v){
if($v=='default') continue;
$size_w_h=explode('x', $v);
$resize_path=img::resize($filepath, $size_w_h[0], $size_w_h[1]);
}
}
}
foreach((array)$water_ary as $v){
img::img_add_watermark($v);
}
$name = substr($_FILES['Filedata']['name'], 0, strrpos($_FILES['Filedata']['name'], '.'));
$status=array(
'status' => 1,
'filepath' => $filepath,
'name' => $name,
);
if($AddPhoto){//是否添加到图片银行
$_img = file::photo_add_item($filepath, $name, $size);
$size!='products' && $status['filepath'] = $_img;//除产品外,全站图片使用图片银行的
}
}
return str::json_data($status);
}
public static function file_upload_ckeditor($save_dir){
$config=array(
'file_type' => array('attach', 'img', 'flash'), //允许上传的文件类型
'img_allow_type' => array('jpg', 'jpeg', 'bmp', 'gif', 'png'), //图片允许上传的格式
'flash_allow_type' => array('swf', 'flv') //flash允许上传的格式
);
$file_type=$_GET['file_type'];
!in_array($file_type, $config['file_type']) && $file_type=$config['file_type'][0];
$fn=(int)$_GET['CKEditorFuncNum'];
if(in_array($file_type, array('img', 'flash')) && !in_array(file::get_ext_name($_FILES['upload']['name']), $config[$file_type.'_allow_type'])){
exit("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction('$fn', '', '".manage::get_language('ckeditor.file_type_err')."');</script>");
}elseif($filepath=file::file_upload($_FILES['upload'], $save_dir)){
exit("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction('$fn', '$filepath', '".manage::get_language('ckeditor.upload_success')."');</script>");
}
exit("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction('$fn', '', '".manage::get_language('ckeditor.upload_fail')."');</script>");
}
public static function write_file($save_dir, $save_name, $contents, $efbbbf=0){ //写文件
if(substr_count(strtolower($save_name), 'php')){
return '';
}else{
file::mk_dir($save_dir);
$fp=fopen(APP_PATH.$save_dir.$save_name, 'w');
fwrite($fp, ($efbbbf==1?pack('H*', 'EFBBBF'):'').$contents);
fclose($fp);
return $save_dir.$save_name;
}
}
public static function del_file($file){ //删除文件
$file=APP_PATH.$file;
if(!$file || !@is_file($file)){return false;}
@unlink($file);
}
public static function del_dir($dir){ //删除文件夹
$handle=@opendir($dir);
while($FolderOrFile=@readdir($handle)){
if($FolderOrFile!="." && $FolderOrFile!=".."){
$fullpath=$dir."/".$FolderOrFile;
if(!@is_dir($fullpath)){
@unlink($fullpath);
}else{
self::del_dir($fullpath);
}
}
}
@closedir($handle);
if(@rmdir($dir)){
return true;
}else{
return false;
}
}
/**
* 清空临时文件夹
* @param $dir
* @param int $time
*/
public static function del_dir_ext($dir,$time=3600){ //删除用户上传的图片但是没有提前的图片。
$handle=@opendir($dir);
while($FolderOrFile=@readdir($handle)){
if($FolderOrFile!="." && $FolderOrFile!=".."){
$fullpath=$dir."/".$FolderOrFile;
if(!@is_dir($fullpath)){
$handle2 = fopen($fullpath,"r");
$fstat = fstat($handle2);
$last_action_time=(int)$fstat["atime"];
fclose($handle2);
if(WEB_TIME-$last_action_time>$time) {
unlink($fullpath);
echo "<script>";
echo "var toObj = document.getElementById('body');";
echo "var newElement = document.createElement('p');";
echo "newElement.innerHTML = '清除成功: " . basename($fullpath) . "';";
echo "document.getElementById('body').insertBefore(newElement,document.getElementById('body').childNodes[0]);";
echo "</script>";
flush();
}
}else{
self::del_dir_ext($fullpath,$time);
}
}
}
@closedir($handle);
}
public static function down_file($filepath, $save_name=''){ //下载文件
$filepath=APP_PATH.$filepath;
!is_file($filepath) && exit();
$save_name=='' && $save_name=basename($filepath);
$file_size=filesize($filepath);
$file_handle=fopen($filepath, 'r');
header("Content-type: application/octet-stream; name=\"$save_name\"\n");
header("Accept-Ranges: bytes\n");
header("Content-Length: $file_size\n");
header("Content-Disposition: attachment; filename=\"$save_name\"\n\n");
while(!feof($file_handle)){
echo fread($file_handle, 1024*100);
}
fclose($file_handle);
}
public function sizecount($filesize) {
if($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' GM';
} elseif($filesize >= 1048576) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
} elseif($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' KB';
} else {
$filesize = $filesize . ' Bytes';
}
return $filesize;
}
/**
* 文件移动
* @param $source
* @param string $save_dir
* @param string $save_name
* @param int $remove_source
* @return string
*/
public static function move_file($source,$save_dir='',$save_name='',$remove_source=1)
{
$save_dir = UPLOAD_FOLDER . $save_dir;
if(!is_file(APP_PATH . $source)){
return '';
}
self::mk_dir($save_dir);
if(!$save_name){
$save_name = str::rand_code().'.'.file::get_ext_name($source);
}
copy(APP_PATH . $source,APP_PATH . $save_dir .$save_name);
//删除原文件
if($remove_source) {
self::del_file($source);
}
return $save_dir . $save_name;
}
/**
* 获取文件类型标识,图片文件原样返回
*/
public static function get_file_icon($file)
{
$icon_ary=array(
'doc' => '/static/manage/img/doc.png',
'docx' => '/static/manage/img/doc.png',
'xls' => '/static/manage/img/excel.png',
'xlsx' => '/static/manage/img/excel.png',
'pdf' => '/static/manage/img/pdf.png'
);
$file_ext = file::get_ext_name($file);
if($file_ext=='jpg' || $file_ext=='png' || $file_ext=='gif' || $file_ext=='jpeg'){
return $file;
}
return $icon_ary[$file_ext];
}
}
?>