Skip to content

Commit

Permalink
File_exists
Browse files Browse the repository at this point in the history
Is_file
Is_dir
  • Loading branch information
wuzhijian committed Jul 24, 2019
1 parent 4a1d15f commit a1895d3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
* Url_decode => url_decode
* Http_build_query => http_build_query
* Uniqid => uniqid
* File_exists => file_exists
* Is_file => is_file
* Is_dir => is_dir

# 使用方法
1. Go module
Expand Down
22 changes: 22 additions & 0 deletions php.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"html"
"io/ioutil"
"net/url"
"os"
"strings"
"time"
"unicode/utf8"
Expand Down Expand Up @@ -275,3 +276,24 @@ func Uniqid(prefix ...string) string {
now := time.Now()
return fmt.Sprintf("%s%08x%05x", pre, now.Unix(), now.UnixNano()%0x100000)
}

func File_exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}

func Is_file(path string) bool {
fi, e := os.Stat(path)
if e != nil {
return false
}
return !fi.IsDir()
}

func Is_dir(path string) bool {
fi, e := os.Stat(path)
if e != nil {
return false
}
return fi.IsDir()
}
14 changes: 14 additions & 0 deletions php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,17 @@ func TestUniqid(t *testing.T) {
t.Log(Uniqid())
t.Log(Uniqid("uid_"))
}

func TestFile_exists(t *testing.T) {
t.Log(File_exists("php.go"))
}

func TestIs_file(t *testing.T) {
t.Error(Is_file("php.go"))
t.Error(Is_file("/Users/wuzhijian"))
}

func TestIs_dir(t *testing.T) {
t.Error(Is_dir("php.go"))
t.Error(Is_dir("/Users/wuzhijian"))
}

0 comments on commit a1895d3

Please sign in to comment.