Skip to content

Commit b460464

Browse files
committed
FOpen, FRead, FClose
1 parent 832e408 commit b460464

1 file changed

Lines changed: 31 additions & 12 deletions

File tree

directory_filesystem.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package phpfuncs
22

33
import (
4+
"bufio"
45
"fmt"
56
"io"
67
"io/ioutil"
8+
"log"
79
"net/http"
810
"os"
911
"path/filepath"
@@ -111,18 +113,35 @@ func FClose(file *os.File) error {
111113
// FOpen - Opens file
112114
// Original : https://www.php.net/manual/en/function.fopen.php
113115
// 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+
}
126145

127146
// FileExists - Checks whether a file or directory exists.
128147
// Original : https://www.php.net/manual/en/function.file-exists.php

0 commit comments

Comments
 (0)