1
- # file ![ License ] ( https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square )
1
+ # amphp/file
2
2
3
- ` amphp/file ` allows non-blocking access to the filesystem for [ Amp] ( https://github.com/amphp/amp ) .
3
+ AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.
4
+ ` amphp/file ` provides non-blocking file system access.
5
+
6
+ [ ![ Latest Release] ( https://img.shields.io/github/release/amphp/file.svg?style=flat-square )] ( https://github.com/amphp/file/releases )
7
+ [ ![ MIT License] ( https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square )] ( https://github.com/amphp/file/blob/master/LICENSE )
4
8
5
9
## Installation
6
10
@@ -10,15 +14,191 @@ This package can be installed as a [Composer](https://getcomposer.org/) dependen
10
14
composer require amphp/file
11
15
```
12
16
13
- ## Optional Extension Backends
17
+ ` amphp/file ` works out of the box without any PHP extensions.
18
+ It uses multiple processes by default, but also comes with a blocking driver that just uses PHP's blocking functions in the current process.
19
+
20
+ Extensions allow using threading in the background instead of using multiple processes:
21
+
22
+ - [ ` ext-eio ` ] ( https://pecl.php.net/package/eio )
23
+ - [ ` ext-uv ` ] ( https://github.com/amphp/ext-uv )
24
+ - [ ` ext-parallel ` ] ( https://github.com/krakjoe/parallel )
25
+
26
+ ## Usage
27
+
28
+ ### read
29
+
30
+ Read the specified file's contents:
31
+
32
+ ``` php
33
+ $contents = Amp\File\read('/path/to/file.txt');
34
+ ```
35
+
36
+ ### write
37
+
38
+ Write the contents string to the specified path:
39
+
40
+ ``` php
41
+ Amp\File\write('/path/to/file.txt', 'contents');
42
+ ```
43
+
44
+ ### openFile
45
+
46
+ Open a [ ` File ` handle] ( #file ) for the specified path, e.g. to stream data:
47
+
48
+ ``` php
49
+ $file = Amp\File\openFile('/path/to/file.txt', 'r');
50
+ Amp\ByteStream\pipe($file, getStdout());
51
+ ```
52
+
53
+ ### getStatus
54
+
55
+ Execute a file stat operation.
56
+
57
+ If the requested path does not exist the function will return ` null ` .
58
+
59
+ ### getLinkStatus
60
+
61
+ Same as [ ` getStatus() ` ] ( #getstatus ) except if the path is a link then the link's data is returned.
62
+
63
+ If the requested path does not exist the function will return ` null ` .
64
+
65
+ ### exists
66
+
67
+ Checks whether the specified path exists.
68
+
69
+ ### getSize
70
+
71
+ Returns the file size in bytes.
72
+
73
+ ### isDirectory
74
+
75
+ Checks whether the given path exists and is a directory.
76
+
77
+ ### isFile
78
+
79
+ Checks whether the given path exists and is a regular file.
80
+
81
+ ### isSymlink
82
+
83
+ Checks whether the given path exists and is a symlink.
84
+
85
+ ### getModificationTime
86
+
87
+ Returns the file's modification time as Unix timestamp in seconds.
88
+
89
+ ### getAccessTime
90
+
91
+ Returns the file's access time as Unix timestamp in seconds.
92
+
93
+ ### getCreationTime
94
+
95
+ Returns the file's creation time as Unix timestamp in seconds.
96
+
97
+ ### createHardlink
98
+
99
+ Creates a new hardlink.
100
+
101
+ ### createSymlink
102
+
103
+ Creates a new symlink.
104
+
105
+ ### resolveSymlink
106
+
107
+ Resolves a symlink to its target path.
108
+
109
+ ### move
110
+
111
+ Move / rename a file or directory.
112
+
113
+ ### deleteFile
114
+
115
+ Deletes a file.
116
+
117
+ ### createDirectory
118
+
119
+ Creates a directory.
120
+
121
+ ### createDirectoriesRecursively
122
+
123
+ Creates a directory and its parents.
124
+
125
+ ### deleteDirectory
126
+
127
+ Deletes a directory.
128
+
129
+ ### listFiles
130
+
131
+ List all files and subdirectories in a directory.
132
+
133
+ ### changePermissions
134
+
135
+ Change the permissions of a file or directory.
136
+
137
+ ### changeOwner
138
+
139
+ Change the ownership of a file or directory.
140
+
141
+ ### touch
142
+
143
+ Update the access and modification time of the specified path.
144
+
145
+ If the file does not exist it will be created automatically.
146
+
147
+ ### File
148
+
149
+ Reference to an open file handle.
150
+
151
+ #### File::read
152
+
153
+ See also ` File::isReadable ` .
154
+
155
+ #### File::write
156
+
157
+ See also ` File::isWritable ` .
158
+
159
+ #### File::end
160
+
161
+ See ` WritableStream::end() ` .
162
+
163
+ #### File::close
164
+
165
+ Close the file handle.
166
+
167
+ - ` File::isClosed() ` can be used to check if the file handle has already been closed.
168
+ - ` File::onClose() ` can be used ot register a callback that's called on file handle closure.
169
+
170
+ #### File::seek
171
+
172
+ Set the internal pointer position.
173
+
174
+ - ` SEEK_SET ` : Set position equal to offset bytes.
175
+ - ` SEEK_CUR ` : Set position to current location plus offset.
176
+ - ` SEEK_END ` : Set position to end-of-file plus offset.
177
+
178
+ Returns the new offset position.
179
+
180
+ See also ` File::isSeekable ` .
181
+
182
+ #### File::tell
183
+
184
+ Return the current internal offset position of the file handle.
185
+
186
+ #### File::eof
187
+
188
+ Test for being at the end of the stream (a.k.a. "end-of-file").
189
+
190
+ #### File::getPath
191
+
192
+ Retrieve the path used when opening the file handle.
193
+
194
+ #### File::getMode
195
+
196
+ Retrieve the mode used when opening the file handle.
14
197
15
- Extensions allow using threading in the background instead of using multiple processes.
16
-
17
- - [ ` ext-eio ` ] ( https://pecl.php.net/package/eio )
18
- - [ ` ext-uv ` ] ( https://github.com/amphp/ext-uv )
19
- - [ ` ext-parallel ` ] ( https://github.com/krakjoe/parallel )
198
+ #### File::truncate
20
199
21
- ` amphp/file ` works out of the box without any PHP extensions. It uses multi-processing by default, but also comes with a blocking driver that just uses PHP's blocking functions in the current process.
200
+ Truncates the file to the given length.
201
+ If ` $size ` is larger than the current file size, the file is extended with NUL bytes.
22
202
23
203
## Versioning
24
204
0 commit comments