|
1 | | -# Leaf FS |
| 1 | +# Filesystem / Cloud Storage |
| 2 | + |
| 3 | +<!-- markdownlint-disable no-inline-html --> |
| 4 | +<!-- # File Storage System --> |
| 5 | + |
| 6 | +A file storage system is a system used to store and manage files. It's a crucial part of most applications, as it helps you create, read, update, store and delete files effectively. Leaf provides a simple and easy-to-use file storage system that allows you to work with files on your server or in the cloud. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +You can quickly install Leaf's file storage system through composer or the leaf cli. |
| 11 | + |
| 12 | +::: code-group |
| 13 | + |
| 14 | +```bash:no-line-numbers [Leaf CLI] |
| 15 | +leaf install fs |
| 16 | +``` |
| 17 | + |
| 18 | +```bash:no-line-numbers [Composer] |
| 19 | +composer require leafs/fs |
| 20 | +``` |
| 21 | + |
| 22 | +::: |
| 23 | + |
| 24 | +That's it! You can now use the `storage()`/`path()` functions from anywhere in your app to interact with your files. |
| 25 | + |
| 26 | +## Working with file paths |
| 27 | + |
| 28 | +File paths are the locations of files on your server. They help you locate and interact with files effectively. While they are essential for working with files, they can be a bit tricky to work with. Leaf provides a simple way to work with file paths using the `path()` function. |
| 29 | + |
| 30 | +### Getting information out of a path |
| 31 | + |
| 32 | +Given a path, you can extract information out of it using those methods: |
| 33 | + |
| 34 | +- `dirname()`: gets the parent folder of a file |
| 35 | +- `basename()`: gets the filename part |
| 36 | +- `extname()`: gets the file extension |
| 37 | + |
| 38 | +```php |
| 39 | +$path = path('path/to/file.txt'); |
| 40 | + |
| 41 | +$path->dirname(); // path/to |
| 42 | +$path->basename(); // file.txt |
| 43 | +$path->extname(); // txt |
| 44 | +``` |
| 45 | + |
| 46 | +### Joining paths |
| 47 | + |
| 48 | +You can join paths together using the `join()` method. |
| 49 | + |
| 50 | +```php |
| 51 | +$path = path('path')->join('to', 'file.txt'); |
| 52 | + |
| 53 | +echo $path; // path/to/file.txt |
| 54 | +``` |
| 55 | + |
| 56 | +### Normalizing paths |
| 57 | + |
| 58 | +When working with paths, we usually tend to do things like `path/to/parent/..//file.txt` or `path\to\file.txt`. Leaf provides a way to "fix" these paths using the `normalize()` method. |
| 59 | + |
| 60 | +```php |
| 61 | +$path = path('path/to/parent/..//file.txt')->normalize(); |
| 62 | + |
| 63 | +echo $path; // path/to/file.txt |
| 64 | +``` |
| 65 | + |
| 66 | +## Working with Files |
| 67 | + |
| 68 | +Working with files is a crucial part of most applications. Leaf provides a simple and easy-to-use file system that allows you to create, read, update, and delete files effectively using the `storage()` function. |
| 69 | + |
| 70 | +### Creating Files |
| 71 | + |
| 72 | +You can create files using the `createFile()` method. It takes in 3 arguments: |
| 73 | + |
| 74 | +- the file path |
| 75 | +- the content to set or a function that returns the file content (optional) |
| 76 | +- an array of configuration options (optional) |
| 77 | + - `overwrite`: whether to overwrite the file if it already exists |
| 78 | + - `rename`: whether to rename the file if it already exists |
| 79 | + |
| 80 | +If you don't provide the file content, Leaf will create an empty file for you. |
| 81 | + |
| 82 | +```php |
| 83 | +storage()->createFile('path/to/file.txt'); |
| 84 | + |
| 85 | +storage()->createFile('path/to/file.txt', function () { |
| 86 | + return 'Hello, world!'; |
| 87 | +}); |
| 88 | + |
| 89 | +storage()->createFile('path/to/file.txt', 'Hello, world!', [ |
| 90 | + 'overwrite' => true |
| 91 | +]); |
| 92 | +``` |
| 93 | + |
| 94 | +The `createFile()` method returns a boolean indicating whether the file was created successfully. If the file was not created successfully, you can get the errors using the `errors()` method. |
| 95 | + |
| 96 | +```php |
| 97 | +$created = storage()->createFile('path/to/file.txt'); |
| 98 | + |
| 99 | +if ($created) { |
| 100 | + echo 'File created successfully'; |
| 101 | +} else { |
| 102 | + $errors = storage()->errors(); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### Reading Files |
| 107 | + |
| 108 | +Reading files means getting the content of a file. You can read files using the `readFile()` method. It takes in the file path and returns the file content. |
| 109 | + |
| 110 | +```php |
| 111 | +$content = storage()->readFile('path/to/file.txt'); |
| 112 | + |
| 113 | +echo $content; |
| 114 | +``` |
| 115 | + |
| 116 | +### Updating Files |
| 117 | + |
| 118 | +You can update files using the `writeFile()` method. It takes in the file path and the content to set or a function that returns the file content. |
| 119 | + |
| 120 | +```php |
| 121 | +storage()->writeFile('path/to/file.txt', 'Hello, world!'); |
| 122 | + |
| 123 | +storage()->writeFile('path/to/file.txt', function () { |
| 124 | + return 'Hello, world!'; |
| 125 | +}); |
| 126 | +``` |
| 127 | + |
| 128 | +If the file is a readable file, the `writeFile()` method will provide the current content of the file to the function. |
| 129 | + |
| 130 | +```php |
| 131 | +storage()->writeFile('path/to/file.txt', function ($content) { |
| 132 | + return $content . ' Hello, world!'; |
| 133 | +}); |
| 134 | +``` |
| 135 | + |
| 136 | +### Getting File Information |
| 137 | + |
| 138 | +You can get information like the file size, file type, and last modified time of a file using the `fileInfo()` method. |
| 139 | + |
| 140 | +```php |
| 141 | +$info = storage()->fileInfo('path/to/file.txt'); |
| 142 | +``` |
| 143 | + |
| 144 | +Or by using more specific methods: |
| 145 | + |
| 146 | +```php |
| 147 | +$size = storage()->fileSize('path/to/file.txt'); // in bytes |
| 148 | +$type = storage()->fileType('path/to/file.txt'); // file type |
| 149 | +$lastModified = storage()->fileLastModified('path/to/file.txt'); // last modified time |
| 150 | +$extension = storage()->extname('path/to/file.txt'); // txt |
| 151 | +$basename = storage()->basename('path/to/file.txt'); // file.txt |
| 152 | +$dirname = storage()->dirname('path/to/file.txt'); // path/to |
| 153 | +``` |
| 154 | + |
| 155 | +## File Uploads |
| 156 | + |
| 157 | +File uploads are a common feature in most applications. Users can upload files like images, videos, and documents to your server. Leaf provides a simple way to handle file uploads using the `upload()` method. |
| 158 | + |
| 159 | +```php |
| 160 | +$uploaded = storage()->upload('fileToUpload', 'path/to/uploads'); |
| 161 | + |
| 162 | +if ($uploaded) { |
| 163 | + echo 'File uploaded successfully'; |
| 164 | +} else { |
| 165 | + $errors = storage()->errors(); |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +The `upload()` method automatically grabs the file from the request, so you don't have to worry about all of that. |
| 170 | + |
| 171 | +One amazing thing about the `upload()` method is that it can detect the file type and automatically handle any associated configuration. If you need to customize the upload configuration, you can pass an array of configuration options as the third parameter. |
| 172 | + |
| 173 | +```php |
| 174 | +$uploaded = storage()->upload('fileToUpload', 'path/to/uploads', [ |
| 175 | + 'maxSize' => 1024 * 1024, // 1MB |
| 176 | + 'allowedTypes' => 'image', |
| 177 | + 'allowedExtensions' => ['jpg', 'png', 'gif'], |
| 178 | + 'rename' => true, |
| 179 | + 'force' => true, |
| 180 | + 'overwrite' => false, |
| 181 | + 'validate' => true, |
| 182 | +]); |
| 183 | +``` |
| 184 | + |
| 185 | +This is a list of config options for the `upload()` method: |
| 186 | + |
| 187 | +| Config Name | Description | Possible Values | |
| 188 | +| :----------- | :----------------------------------------------------------- | ------------------------------------------------------------------------------------: | |
| 189 | +| force | If `true`, Leaf will create the folder if it doesn't exist | `true`, `false` | |
| 190 | +| overwrite | If `true`, Leaf will overwrite the file if it already exists | `true`, `false` | |
| 191 | +| rename | If `true`, Leaf will rename the file if it already exists | `true`, `false` | |
| 192 | +| validate | If `true`, Leaf will validate the file before uploading | `true`, `false` | |
| 193 | +| maxSize | The maximum file size allowed in bytes | `int` | |
| 194 | +| allowedTypes | The allowed file types | `image`, `video`, `audio`, `presentation`, `compressed`, `spreadsheet`, `application`, `custom` | |
| 195 | + |
| 196 | +This is a list of values Leaf uses to check for file types: |
| 197 | + |
| 198 | +| File Type | Common Extensions | |
| 199 | +| :-------------- | :---------------------------------------------- | |
| 200 | +| image | 'jpg', 'jpeg', 'png', 'gif', 'webp', 'apng', 'tif', 'tiff', 'svg', 'pjpeg', 'pjp', 'jfif', 'cur', 'ico' | |
| 201 | +| video | 'mp4', 'webm', 'swf', 'flv' | |
| 202 | +| audio |'wav', 'mp3', 'ogg', 'm4a' | |
| 203 | +| text |'txt', 'log', 'xml', 'doc', 'docx', 'odt', 'wpd', 'rtf', 'tex', 'pdf' | |
| 204 | +| presentation |'ppsx', 'pptx', 'ppt', 'pps', 'ppsm', 'key', 'odp' | |
| 205 | +| compressed |'zip', 'rar', 'bz', 'gz', 'iso', 'tar.gz', 'tgz', 'zipx', '7z', 'dmg'| |
| 206 | +| spreadsheet |'ods', 'xls', 'xlsx', 'xlsm' | |
| 207 | +| application |'apk', 'bat', 'cgi', 'pl', 'com', 'exe', 'gadget', 'jar', 'msi', 'py', 'wsf' | |
| 208 | + |
| 209 | +## Working with Folders |
| 210 | + |
| 211 | +Working with folders is an essential part of most applications. Leaf provides a simple and easy-to-use file system that allows you to create, read, update, and delete folders effectively using the `storage()` function. |
| 212 | + |
| 213 | +### Creating Folders |
| 214 | + |
| 215 | +You can create folders using the `createFolder()` method. It takes in 2 parameters: |
| 216 | + |
| 217 | +- the folder path |
| 218 | +- an array of configuration options. |
| 219 | + - `recursive`: whether to create the folder recursively |
| 220 | + - `rename`: whether to rename the folder if a folder with the same name already exists |
| 221 | + |
| 222 | +```php |
| 223 | +storage()->createFolder('path/to/folder'); |
| 224 | + |
| 225 | +storage()->createFolder('path/to/folder', [ |
| 226 | + 'recursive' => true |
| 227 | +]); |
| 228 | +``` |
| 229 | + |
| 230 | +It returns a boolean indicating whether the folder was created successfully. |
| 231 | + |
| 232 | +```php |
| 233 | +$created = storage()->createFolder('path/to/folder', [ |
| 234 | + 'rename' => true |
| 235 | +]); |
| 236 | + |
| 237 | +if ($created) { |
| 238 | + echo 'Folder created successfully'; |
| 239 | +} else { |
| 240 | + $errors = storage()->errors(); |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +### Checking Folder Contents |
| 245 | + |
| 246 | +You can check if a folder is empty using the `isEmpty()` method. It takes in the folder path and returns a boolean indicating whether the folder is empty. |
| 247 | + |
| 248 | +```php |
| 249 | +$isEmpty = storage()->isEmpty('path/to/folder'); |
| 250 | +``` |
| 251 | + |
| 252 | +You can also get the contents of a folder using the `listDir()` method. It takes in the folder path and a key to sort the contents by. It returns an array of the folder contents. |
| 253 | + |
| 254 | +```php |
| 255 | +$contents = storage()->listDir('path/to/folder'); |
| 256 | +$phpFiles = storage()->listDir('path/to/folder', '*.php'); |
| 257 | +``` |
| 258 | + |
| 259 | +If you need to do more complex filtering, you can pass a function as the second parameter to the `listDir()` method. |
| 260 | + |
| 261 | +```php |
| 262 | +$contents = storage()->listDir('path/to/folder', function ($file) { |
| 263 | + // if true, the file will be included in the results |
| 264 | + return $file->isFile() && $file->extname() === 'php'; |
| 265 | +}); |
| 266 | +``` |
| 267 | + |
| 268 | +<!-- ## Working with Cloud Storage --> |
| 269 | + |
| 270 | +## Renaming Files and Folders |
| 271 | + |
| 272 | +There are instances where you might need to rename files or folders. Leaf provides a simple way to rename files and folders using the `rename()` method. It takes in 2 parameters: |
| 273 | + |
| 274 | +- the current file/folder path |
| 275 | +- the new file/folder path |
| 276 | + |
| 277 | +```php |
| 278 | +storage()->rename('path/to/file.txt', 'path/to/new-file.txt'); |
| 279 | + |
| 280 | +storage()->rename('path/to/folder', 'path/to/new-folder'); |
| 281 | +``` |
| 282 | + |
| 283 | +It returns a boolean indicating whether the file/folder was renamed successfully. |
| 284 | + |
| 285 | +```php |
| 286 | +$renamed = storage()->rename('path/to/file.txt', 'path/to/new-file.txt'); |
| 287 | + |
| 288 | +if ($renamed) { |
| 289 | + echo 'File renamed successfully'; |
| 290 | +} else { |
| 291 | + $errors = storage()->errors(); |
| 292 | +} |
| 293 | +``` |
| 294 | + |
| 295 | +## Deleting Files and Folders |
| 296 | + |
| 297 | +When you no longer need a file or folder, you can delete it using the `delete()` method. It takes in the file/folder path and returns a boolean indicating whether the file/folder was deleted successfully. |
| 298 | + |
| 299 | +```php |
| 300 | +$deleted = storage()->delete('path/to/file.txt'); |
| 301 | + |
| 302 | +if ($deleted) { |
| 303 | + echo 'File deleted successfully'; |
| 304 | +} else { |
| 305 | + $errors = storage()->errors(); |
| 306 | +} |
| 307 | +``` |
| 308 | + |
| 309 | +## Copying Files and Folders |
| 310 | + |
| 311 | +When you need to duplicate a file or folder, you can copy it using the `copy()` method. It takes in 2 parameters: |
| 312 | + |
| 313 | +- the current file/folder path |
| 314 | +- the new file/folder path |
| 315 | + |
| 316 | +```php |
| 317 | +storage()->copy('path/to/file.txt', 'path/to/new-file.txt'); |
| 318 | + |
| 319 | +storage()->copy('path/to/folder', 'path/to/new-folder'); |
| 320 | +``` |
| 321 | + |
| 322 | +It returns a boolean indicating whether the file/folder was copied successfully. |
| 323 | + |
| 324 | +```php |
| 325 | +$copied = storage()->copy('path/to/file.txt', 'path/to/new-file.txt'); |
| 326 | + |
| 327 | +if ($copied) { |
| 328 | + echo 'File copied successfully'; |
| 329 | +} else { |
| 330 | + $errors = storage()->errors(); |
| 331 | +} |
| 332 | +``` |
| 333 | + |
| 334 | +## Moving Files and Folders |
| 335 | + |
| 336 | +This is similar to copying files and folders, but instead of duplicating the file/folder, it moves it to a new location. You can move files and folders using the `move()` method. It takes in 2 parameters: |
| 337 | + |
| 338 | +- the current file/folder path |
| 339 | +- the new file/folder path |
| 340 | + |
| 341 | +```php |
| 342 | +storage()->move('path/to/file.txt', 'path/to/new-file.txt'); |
| 343 | + |
| 344 | +storage()->move('path/to/folder', 'path/to/new-folder'); |
| 345 | +``` |
| 346 | + |
| 347 | +It returns a boolean indicating whether the file/folder was moved successfully. |
| 348 | + |
| 349 | +```php |
| 350 | +$moved = storage()->move('path/to/file.txt', 'path/to/new-file.txt'); |
| 351 | + |
| 352 | +if ($moved) { |
| 353 | + echo 'File moved successfully'; |
| 354 | +} else { |
| 355 | + $errors = storage()->errors(); |
| 356 | +} |
| 357 | +``` |
| 358 | + |
| 359 | +<!-- ## Working with Streams |
| 360 | +
|
| 361 | +Streaming is a way to read and write data from a source or to a destination. Leaf provides a simple way to work with streams using the `stream()` function. |
| 362 | +
|
| 363 | +```php |
| 364 | +$stream = storage()->stream('path/to/file.txt'); |
| 365 | +
|
| 366 | +$stream->write('Hello, world!'); |
| 367 | +$stream->read(); |
| 368 | +``` --> |
0 commit comments