|
1 | 1 | package phpfuncs |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "fmt" |
5 | 6 | "io" |
6 | 7 | "io/ioutil" |
| 8 | + "log" |
7 | 9 | "net/http" |
8 | 10 | "os" |
9 | 11 | "path/filepath" |
@@ -111,18 +113,35 @@ func FClose(file *os.File) error { |
111 | 113 | // FOpen - Opens file |
112 | 114 | // Original : https://www.php.net/manual/en/function.fopen.php |
113 | 115 | // fopen() binds a named resource, specified by filename, to a stream. |
114 | | -// NOT COMPLETED |
115 | | -// func FOpen(file string, mode int) (os.file, error) { |
116 | | -// f, err := os.OpenFile(file, mode, 0644) |
117 | | -// if err != nil { |
118 | | -// log.Fatal(err) |
119 | | -// } |
120 | | -// if err := f.Close(); err != nil { |
121 | | -// log.Fatal(err) |
122 | | -// } |
123 | | - |
124 | | -// defer file.Close() |
125 | | -// } |
| 116 | +// Mode : os.O_RDONLY | os.O_WRONLY | os.O_RDWR | os.O_APPEND | os.O_CREATE | os.O_EXCL | os.O_SYNC | os.O_TRUNC |
| 117 | +func FOpen(file string, mode int) (f *os.File) { |
| 118 | + f, err := os.OpenFile(file, mode, 0644) |
| 119 | + if err != nil { |
| 120 | + log.Fatal(err) |
| 121 | + } |
| 122 | + return f |
| 123 | +} |
| 124 | + |
| 125 | +// FRead - Binary-safe file read. |
| 126 | +// Original : https://www.php.net/manual/en/function.fread.php |
| 127 | +// fread() reads up to length bytes from the file pointer referenced by handle. |
| 128 | +func FRead(f *os.File, sb int64) string { |
| 129 | + r := bufio.NewReader(f) |
| 130 | + b := make([]byte, sb) |
| 131 | + var cls string |
| 132 | + |
| 133 | + for { |
| 134 | + n, err := r.Read(b) |
| 135 | + if err != nil { |
| 136 | + if err != io.EOF { |
| 137 | + fmt.Println(err) |
| 138 | + } |
| 139 | + break |
| 140 | + } |
| 141 | + cls += string(b[0:n]) |
| 142 | + } |
| 143 | + return cls |
| 144 | +} |
126 | 145 |
|
127 | 146 | // FileExists - Checks whether a file or directory exists. |
128 | 147 | // Original : https://www.php.net/manual/en/function.file-exists.php |
@@ -183,6 +202,12 @@ func FileType(fs string) (string, error) { |
183 | 202 | return contentType, nil |
184 | 203 | } |
185 | 204 |
|
| 205 | +// Glob - Find pathnames matching a pattern. |
| 206 | +// Original : https://www.php.net/manual/en/function.glob.php |
| 207 | +// The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells. |
| 208 | +func Glob(path string) (matches []string, err error) { |
| 209 | + return filepath.Glob(path) |
| 210 | +} |
186 | 211 |
|
187 | 212 | // IsDir - Tells whether the filename is a directory. |
188 | 213 | // Original : https://www.php.net/manual/en/function.is-dir.php |
@@ -240,7 +265,54 @@ func MkDir(path string, mode os.FileMode) error { |
240 | 265 | return os.Mkdir(path, mode) |
241 | 266 | } |
242 | 267 |
|
| 268 | +// ReadLink - Returns the target of a symbolic link. |
| 269 | +// Original : https://www.php.net/manual/en/function.readlink.php |
| 270 | +// readlink() does the same as the readlink C function. |
| 271 | +func ReadLink(path string) (string, error){ |
| 272 | + li, err := os.Readlink(path) |
| 273 | + if err != nil { |
| 274 | + return "", err |
| 275 | + } |
| 276 | + return li, err |
| 277 | +} |
| 278 | + |
| 279 | +// RealPath - Returns canonicalized absolute pathname. |
| 280 | +// Original : https://www.php.net/manual/en/function.realpath.php |
| 281 | +// realpath() expands all symbolic links and resolves references to /./, /../ and extra / characters in the input path and returns the canonicalized absolute pathname. |
| 282 | +func RealPath(path string) (string, error) { |
| 283 | + return filepath.Abs(path) |
| 284 | +} |
| 285 | + |
| 286 | +// Rename - Renames a file or directory. |
| 287 | +// Original : https://www.php.net/manual/en/function.rename.php |
| 288 | +// Attempts to rename oldname to newname, moving it between directories if necessary. If renaming a file and newname exists, it will be overwritten. If renaming a directory and newname exists, this function will emit a warning. |
| 289 | +func Rename(oldpath, newpath string) error { |
| 290 | + return os.Rename(oldpath, newpath) |
| 291 | +} |
| 292 | + |
| 293 | +// RmDir — Removes directory. |
| 294 | +// Original : https://www.php.net/manual/en/function.rmdir.php |
| 295 | +// Attempts to remove the directory named by dirname. The directory must be empty, and the relevant permissions must permit this. A E_WARNING level error will be generated on failure. |
| 296 | +func RmDir(path string) error { |
| 297 | + return os.RemoveAll(path) |
| 298 | +} |
| 299 | + |
| 300 | +// Stat - Gives information about a file. |
| 301 | +// Original : https://www.php.net/manual/en/function.stat.php |
| 302 | +// Gathers the statistics of the file named by filename. If filename is a symbolic link, statistics are from the file itself, not the symlink. Prior to PHP 7.4.0, on Windows NTS builds the size, atime, mtime and ctime statistics have been from the symlink, in this case. |
| 303 | +func Stat(name string) (os.FileInfo, error) { |
| 304 | + return os.Stat(name) |
| 305 | +} |
| 306 | + |
| 307 | +// Unlink - Deletes a file. |
| 308 | +// Original : https://www.php.net/manual/en/function.unlink.php |
| 309 | +// Deletes filename. Similar to the Unix C unlink() function. An E_WARNING level error will be generated on failure. |
| 310 | +func Unlink(name string) error { |
| 311 | + return Delete(name) |
| 312 | +} |
| 313 | + |
243 | 314 | // ByteCountIEC - Bytecount & Humanize Bytes |
| 315 | +// Complete calculator for DiskFreeSize |
244 | 316 | func ByteCountIEC(b uint64) string { |
245 | 317 | const unit = 1024 |
246 | 318 | if b < unit { |
|
0 commit comments