CDir.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fec\helpers;
  10. use Yii;
  11. use yii\base\InvalidValueException;
  12. /**
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class CDir
  17. {
  18. # 1.得到web路径在linux下面的地址
  19. public static function getBaseDir(){
  20. return Yii::getAlias('@webroot');
  21. }
  22. # 2.得到 @webroot/media
  23. public static function getMediaDir(){
  24. return self::getBaseDir()."/media";
  25. }
  26. /**
  27. * @property $baseDir | String, create folder in this dir.
  28. * @property $createdDir| String, create folder
  29. * Example: $baseDir = '/www/web/fecshop/web',$createdDir='a/b/c';
  30. * this function return /www/web/fecshop/web/a/b/c
  31. */
  32. public static function createFloder($baseDir,$createdDir){
  33. if(!is_dir($baseDir)){
  34. throw new InvalidValueException('base dir is not a correct dir');
  35. }
  36. if(!$createdDir){
  37. throw new InvalidValueException('created dir can not empty');
  38. }
  39. if(!is_array($createdDir)){
  40. $createdDir = trim($createdDir,'/');
  41. $dir_arr = explode('/',$createdDir);
  42. }else{
  43. $dir_arr = $createdDir;
  44. }
  45. $dir = $baseDir;
  46. foreach($dir_arr as $folder){
  47. $dir = $dir.'/'.$folder;
  48. if(!is_dir($dir)){
  49. @mkdir($dir,0777);
  50. }
  51. }
  52. if(is_dir($dir)){
  53. return $dir;
  54. }else{
  55. return false;
  56. }
  57. }
  58. }