file.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package file
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. )
  9. // Basename get file basename
  10. // php basename
  11. func Basename(fPath string) string {
  12. return filepath.Base(fPath)
  13. }
  14. // Dirname get file dirname
  15. // php dirname
  16. func Dirname(fPath string) string {
  17. return filepath.Dir(fPath)
  18. }
  19. // Filesize get file size
  20. // php filesize
  21. func Filesize(fPath string) int64 {
  22. fInfo, err := os.Stat(fPath)
  23. if err != nil && os.IsNotExist(err) {
  24. return 0
  25. }
  26. fm := fInfo.Mode()
  27. if fm.IsDir() {
  28. return 0
  29. }
  30. return fInfo.Size()
  31. }
  32. // PathInfo get file all info
  33. // php pathinfo
  34. func PathInfo(fPath string) (map[string]interface{}, error) {
  35. fInfoMap := map[string]interface{}{}
  36. fInfo, err := os.Stat(fPath)
  37. if err != nil && os.IsNotExist(err) {
  38. return fInfoMap, err
  39. }
  40. fm := fInfo.Mode()
  41. if fm.IsDir() {
  42. return fInfoMap, errors.New("fPath is dirname")
  43. }
  44. fInfoMap["dirname"] = filepath.Dir(fPath)
  45. fInfoMap["filename"] = fInfo.Name()
  46. fInfoMap["size"] = fInfo.Size()
  47. fInfoMap["extension"] = path.Ext(fPath)
  48. // fInfoMap["mode"] = fInfo.Mode()
  49. return fInfoMap, nil
  50. }
  51. // FileExists check file is exists
  52. // php file_exists
  53. func FileExists(fName string) bool {
  54. _, err := os.Stat(fName)
  55. if err != nil && os.IsNotExist(err) {
  56. return false
  57. }
  58. return true
  59. }
  60. // IsDir fileName is dir
  61. // php is_dir
  62. func IsDir(fName string) bool {
  63. fInfo, err := os.Stat(fName)
  64. if err != nil {
  65. return false
  66. }
  67. fm := fInfo.Mode()
  68. return fm.IsDir()
  69. }
  70. // IsFile fileName is file
  71. // php is_file
  72. func IsFile(fName string) bool {
  73. fInfo, err := os.Stat(fName)
  74. if err != nil && os.IsNotExist(err) {
  75. return false
  76. }
  77. fm := fInfo.Mode()
  78. return !fm.IsDir()
  79. }
  80. // FileGetContents read file content
  81. //php file_get_contents
  82. func FileGetContents(fName string) (string, error) {
  83. data, err := ioutil.ReadFile(fName)
  84. return string(data), err
  85. }
  86. // FilePutContents write file content
  87. // php file_put_contents
  88. func FilePutContents(filename string, data string, mode os.FileMode) error {
  89. return ioutil.WriteFile(filename, []byte(data), mode)
  90. }
  91. // Chmod chmod_file mode
  92. // php chmod
  93. func Chmod(filename string, mode os.FileMode) bool {
  94. return os.Chmod(filename, mode) == nil
  95. }
  96. // Chown chown_file
  97. // php chown
  98. func Chown(filename string, uid, gid int) bool {
  99. return os.Chown(filename, uid, gid) == nil
  100. }