几个常用函数(切除字符串,时间处理,文件上传)

public static function cutString($string, $length) {
    $len = strlen($string);
    if ($len > $length) {
        $part_len = round(($length - 5) / 4);
        $string = substr($string, 0, $part_len * 3).' ... '.substr($string, - $part_len, $part_len);
    }
    return $string;
}
/*
   时间戳的相对时间
*/
public static function relativeTime($time) {
    $divisions  = array(1, 60, 60, 24, 7, 4.34, 12);
    $names = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
    $time = time() - $time;
    $name = '';
    if ($time < 10) {
        return "just now";
    }
    for ($i = 0; $i < count($divisions); $i++) {
        if ($time < $divisions[$i]) break;
        $time = $time/$divisions[$i];
        $name = $names[$i];
    }
    $time = round($time);
    if ($time != 1) {
        $name .= 's';
    }
    return "$time $name ago";
}
/**
 * email 正确格式验证
 *
 * @param string  $email
 * @return bool
 */
public static function isEmail($email) {
    if (preg_match("/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$/", $email)) {
        return true;
    } else {
        return false;
    }
}


/**
 * Phone 正确格式验证
 *
 * @param string  $phone
 * @return bool
 */
public static function isPhone($phone) {
    if (preg_match("/^(1(([35][0-9])|(47)|[8][01236789]))\\d{8}$/", $phone)) {
        return true;
    } else {
        return false;
    }
}
/**
 * HTML头信息 - 缓存
 *
 * @param int     $expires (optional)
 */
public static function cacheHeader($expires=900) {
    @header('Last-Modified: ' . @gmdate("D, d M Y H:i:s", floor(@time() / $expires) * $expires) . ' GMT');
    @header('Cache-Control: max-age=' . $expires . ', must-revalidate');
    @header('Expires: ' . @gmdate("D, d M Y H:i:s", @time() + $expires) . ' GMT');
}


/**
 * HTML头信息 - 禁用缓存
 */
public static function noCacheHeader() {
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Pragma: no-cache");
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
}
/**
 * 获得文件后缀
 *
 * @param string  $file_name
 * @return string
 */
public static function getFileExtension($file_name) {
    if($file_name) {
        $file_arr = pathinfo($file_name);
        return $file_arr['extension'];
    } else {
        return '';
    }
}
/**
   * 上传图片,banner
   * @param file $file
   * @param string $save_path
   * @param string $imagename
   */
  public static function upload_Image($file, $save_path, $image_name='', $rename='') {
      if(!file_exists($file)) return false;
      //if(!self::createDir($save_path)) return false;
      if(!file_exists($save_path)) {
          if(!mkdir($save_path, 0777, true)) return array("error"=>self::strError("Upload directory `".$save_path."` created failed!"));
      }
      $image_types = array(1 => 'gif', 2 => 'jpg', 3 => 'png');

      list($width, $height, $file_type) = getimagesize($file);
      $file_size = @filesize($file);
      $extension = $image_types[$file_type];
      if ($file_size < 1024) return array("error"=>self::strError("The images was smaller than 1MB. Please upload a larger images."));;
      if ($file_size > 2048*1024) return array("error"=>self::strError("The images was larger than 2MB. Please upload a smaller images."));
      if ($width < 10 || $width > 3000 || $height < 10 || $height > 5000) return false;

      if($rename) {
          $uploadDir = $rename;
      } else {
          $uploadDir = time();
      }
$uploadDir .= '.'.$extension;
      if ($image_name!='' && file_exists($save_path.$image_name)) {
          unlink($save_path.$image_name);
      }
      if(move_uploaded_file($file, $save_path.$uploadDir)) {
          return $uploadDir."?v=".time();
      } else {
          return false;
      }
  }
  /**
   * 文件上传
   *
   * @author Roble
   * @param string  $dir_path  文件存储路径
   * @param string  $file_name 文件名(code)
   * @param string  $tmp_file  上传的文件信息
   * @param int  $playmode 游戏类别,=3时,为html5游戏需要在解包时,将分享侧栏代码加入游戏首页.默认为0
   * @param int  $type      是否生成特别图案的封面图,0 不生成 1 六边形
   * @param string  $url      (optional) 旧路径
   * @param string $postfix    新文件名的后缀
   * @created 2014-2-19
   */
  public static function uploadFile($dirPath, $fileName, $tmpFile, $playmode=0, $type=0, $url='', $postfix='') {        //判断文件是否存在或者是否是文件目录
      $storeFile =  self::getNewGameUrl($fileName);
      $dirName = $dirPath.$storeFile.$fileName;
      if(!file_exists($dirName)) {
       self::createDir($dirName);//不存在则创建目录
          if(!file_exists($dirName)){
              return array("error"=>self::strError("Upload directory `".$fileName."` created failed!"));
          }
      }
      //上传文件信息异常判断
      if($tmpFile != '') {
          if($tmpFile->error == 1) {
              return  array("error"=>self::strError("Upload file size is greater than the server Settings, please contact the administrator."));
          }
          /*获得上传的后缀(提供的文件后缀可能与检测的不一致,但是要保持上传的后缀一致)*/
          $fileSuffix = self::getFileExtension($tmpFile->name);
          $fileSuf = self::fileTypes($fileSuffix);
          //判断文件类型
          if($fileSuf <= 3) {
              if($tmpFile->size > 5 * 1024 * 1024) {
                  return array("error"=>self::strError("The images was larger than 5MB. Please upload a smaller images."));
              }
              if(!$fileSuf) {
                  return  array(
                  "error"=>self::strError("Invalid file format upload attempt."));
              }
              $dirUrl = $fileName.self::iif($postfix!='',$postfix).'.'.$fileSuffix;
          } elseif($fileSuf >=4) {
              if($tmpFile->size > 80 * 1024 * 1024) {
                  return array("error"=>self::strError("The file was larger than 80MB. Please upload a smaller file."));
              }
              $dirUrl = $fileName.".".$fileSuffix;
          }
          //上传完整路径
          $uploadDir = $dirName."/".$dirUrl;
          //判断目录是否存在
          if(is_dir($dirName)) {
              if($url) {//判断是否有旧的文件存在
                  if(file_exists($dirPath.$url)){
                      //删除旧的文件
                      var_dump($dirName.$url);exit;
                      unlink($dirName.$url);
                  }
              }
              if(move_uploaded_file($tmpFile->tempName, $uploadDir)) {
                  if($type==1) {
                      self::diamondPattern($dirName,$dirUrl);
                  }
                  if($playmode==3) {
                   /*解压zip*/
                   if($fileSuf === 7 || $fileSuf === 8) {
                       //delete index.php
                       if (file_exists($dirName."/index.php")) {
                           unlink($dirName."/index.php");
                       }
                       if($fileSuf === 7) {
                           //rar文件包
                           $rar_file = rar_open($uploadDir) or die("Can't open Rar archive");
                           $entries = rar_list($rar_file);
                           foreach ($entries as $entry) {
                               $entry->extract($dirName);
                           }
                           rar_close($rar_file);
                       } elseif ($fileSuf === 8) {//解压Zip                           include("../protected/extensions/pclzip.lib.php");//引用zip扩展库
                           $archive = new PclZip($uploadDir);
                           if (!$archive->extract(PCLZIP_OPT_PATH, $dirName."/")){
                               return array(
                               "error"=>self::strError("Can't open Zip archive."));
                           }
                       }
                       //删除压缩包文件
                       if(file_exists($uploadDir)) {
                           $result = unlink($uploadDir);
                       }
                          //self::rrmdir($uploadDir,'delImpermissionFile');
                          rename($dirName."/index.html", $dirName."/index.php");
                          self::AddShareBar($dirName.'/index.php');
                          return array("url"=>$storeFile.$fileName."/index.php");
                   }
                  } else {
                          //return array("url"=>"");
                  }
                  return array("url"=>$storeFile.$fileName."/".$dirUrl."?v=".time());
              }else{
                  return array("error"=>self::strError($tmpFile->tempName."|".$uploadDir));
              }
          }else{
              return array("error"=>self::strError("No such file or directory."));
          }
      }
  }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值