Skip to content

Commit e613fed

Browse files
committed
feat: add multi-file upload docs
1 parent 6a94aa6 commit e613fed

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

src/docs/utils/fs.md

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ $dirname = storage()->dirname('path/to/file.txt'); // path/to
154154

155155
## File Uploads
156156

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.
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 right on the request.
158158

159159
```php
160-
$uploaded = storage()->upload('fileToUpload', 'path/to/uploads');
160+
$uploaded = request()->upload('fileToUpload', 'path/to/uploads');
161161

162162
if ($uploaded) {
163163
echo 'File uploaded successfully';
164164
} else {
165-
$errors = storage()->errors();
165+
$errors = request()->errors();
166166
}
167167
```
168168

@@ -171,7 +171,7 @@ The `upload()` method automatically grabs the file from the request, so you don'
171171
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.
172172

173173
```php
174-
$uploaded = storage()->upload('fileToUpload', 'path/to/uploads', [
174+
$uploaded = request()->upload('fileToUpload', 'path/to/uploads', [
175175
'maxSize' => 1024 * 1024, // 1MB
176176
'allowedTypes' => ['image'],
177177
'allowedExtensions' => ['jpg', 'png', 'gif'],
@@ -207,6 +207,75 @@ This is a list of values Leaf uses to check for file types:
207207
| spreadsheet | 'ods', 'xls', 'xlsx', 'xlsm' |
208208
| application | 'apk', 'bat', 'cgi', 'pl', 'com', 'exe', 'gadget', 'jar', 'msi', 'py', 'wsf' |
209209

210+
When the file is uploaded, Leaf will return information about the uploaded file, or `false` if the file was not uploaded successfully.
211+
212+
```php
213+
if ($uploaded) {
214+
echo 'File uploaded successfully';
215+
echo $uploaded['name']; // file name
216+
echo $uploaded['path']; // file path
217+
echo $uploaded['size']; // file size
218+
echo $uploaded['type']; // file type
219+
echo $uploaded['extension']; // file extension
220+
} else {
221+
$errors = request()->errors();
222+
}
223+
```
224+
225+
If your `.env` file has an `APP_URL` configured, Leaf will add the `APP_URL` to the file path under the `url` key.
226+
227+
```php
228+
if ($uploaded) {
229+
echo 'File uploaded successfully';
230+
echo $uploaded['url']; // file url -> http://yourapp.com/path/to/uploads/file.txt
231+
}
232+
```
233+
234+
## Uploading multiple files <Badge>NEW</Badge>
235+
236+
You may need to allow users enter multiple files at once on the same input, for example, uploading multiple documents to a teacher's portal. Leaf's `upload()` now automatically handles multiple files under the same input.
237+
238+
::: code-group
239+
240+
```html:no-line-numbers [Input]
241+
<input type="file" name="files[]" multiple>
242+
```
243+
244+
```javascript:no-line-numbers [FormData]
245+
const formData = new FormData();
246+
formData.append('files[]', file1);
247+
formData.append('files[]', file2);
248+
249+
...
250+
```
251+
252+
:::
253+
254+
And then your handler will look like this:
255+
256+
```php:no-line-numbers
257+
$uploaded = request()->upload('files', 'path/to/uploads');
258+
```
259+
260+
The value of `$uploaded` will be an array of the uploaded files.
261+
262+
```php
263+
if ($uploaded) {
264+
echo 'Files uploaded successfully';
265+
266+
foreach ($uploaded as $file) {
267+
echo $file['name']; // file name
268+
echo $file['path']; // file path
269+
echo $file['size']; // file size
270+
echo $file['type']; // file type
271+
echo $file['extension']; // file extension
272+
echo $file['url']; // file url -> http://yourapp.com/path/to/uploads/file.txt
273+
}
274+
} else {
275+
$errors = request()->errors();
276+
}
277+
```
278+
210279
## Working with Folders
211280

212281
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.

0 commit comments

Comments
 (0)