You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@
3
3
## 0.20.1 (unreleased)
4
4
5
5
- More than 200 new icons, with [tabler icons v3](https://tabler.io/icons/changelog#3.0)
6
+
- New [`sqlpage.persist_uploaded_file`](https://sql.ophir.dev/functions.sql?function=persist_uploaded_file#function) function to save uploaded files to a permanent location on the local filesystem (where SQLPage is running). This is useful to store files uploaded by users in a safe location, and to serve them back to users later.
7
+
- Correct error handling for file uploads. SQLPage used to silently ignore file uploads that failed (because they exceeded [max_uploaded_file_size](./configuration.md), for instance), but now it displays a clear error message to the user.
When the uploaded file is larger than a few megabytes, it is not recommended to store it in the database.
49
49
Instead, one can save the file to a permanent location on the server, and store the path to the file in the database.
50
50
51
-
You can move the file to a permanent location using the [`sqlpage.exec`](?function=exec#function) function:
52
-
53
-
```sql
54
-
set file_name = sqlpage.random_string(10);
55
-
set exec_result = sqlpage.exec(''mv'', sqlpage.uploaded_file_path(''myfile''), ''/my_upload_directory/'' || $file_name);
56
-
insert into uploaded_files (title, path) values (:title, $file_name);
57
-
```
58
-
59
-
> *Notes*:
60
-
> - The `sqlpage.exec` function is disabled by default, and you need to enable it in the [configuration file](https://github.com/lovasoa/SQLpage/blob/main/configuration.md).
61
-
> - `mv` is specific to MacOS and Linux. On Windows, you can use [`move`](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/move) instead:
62
-
> - ```sql
63
-
> SET image_path = sqlpage.uploaded_file_path(''myfile'');
64
-
> SET exec_result = sqlpage.exec(''cmd'', ''/C'', ''move'', $image_path, ''C:\MyUploadDirectory'');
65
-
> ```
66
-
51
+
You can move the file to a permanent location using the [`sqlpage.persist_uploaded_file`](?function=persist_uploaded_file#function) function.
67
52
### Advanced file handling
68
53
69
54
For more advanced file handling, such as uploading files to a cloud storage service,
70
55
you can write a small script in your favorite programming language,
71
-
and call it using the `sqlpage.exec` function.
56
+
and call it using the [`sqlpage.exec`](?function=exec#function) function.
72
57
73
58
For instance, one could save the following small bash script to `/usr/local/bin/upload_to_s3`:
'Persists an uploaded file to the local filesystem, and returns its path.
12
+
13
+
### Example
14
+
15
+
#### User profile picture
16
+
17
+
##### `upload_form.sql`
18
+
19
+
```sql
20
+
select ''form'' as component, ''persist_uploaded_file.sql'' as action;
21
+
select ''file'' as type, ''profile_picture'' as name, ''Upload your profile picture'' as label;
22
+
```
23
+
24
+
##### `persist_uploaded_file.sql`
25
+
26
+
```sql
27
+
update user
28
+
set profile_picture = sqlpage.persist_uploaded_file(''profile_picture'', ''profile_pictures'', ''jpg,jpeg,png,gif,webp'')
29
+
where id = (
30
+
select user_id from session where session_id = sqlpage.cookie(''session_id'')
31
+
);
32
+
```
33
+
34
+
'
35
+
);
36
+
INSERT INTO sqlpage_function_parameters (
37
+
"function",
38
+
"index",
39
+
"name",
40
+
"description_md",
41
+
"type"
42
+
)
43
+
VALUES (
44
+
'persist_uploaded_file',
45
+
1,
46
+
'file',
47
+
'Name of the form field containing the uploaded file. The current page must be referenced in the `action` property of a `form` component that contains a file input field.',
48
+
'TEXT'
49
+
),
50
+
(
51
+
'persist_uploaded_file',
52
+
2,
53
+
'destination_folder',
54
+
'Optional. Path to the folder where the file will be saved, relative to the web root (the root folder of your website files). By default, the file will be saved in the `uploads` folder.',
55
+
'TEXT'
56
+
),
57
+
(
58
+
'persist_uploaded_file',
59
+
3,
60
+
'allowed_extensions',
61
+
'Optional. Comma-separated list of allowed file extensions. By default: jpg,jpeg,png,gif,bmp,webp,pdf,txt,doc,docx,xls,xlsx,csv,mp3,mp4,wav,avi,mov.
62
+
Changing this may be dangerous ! If you add "sql", "svg" or "html" to the list, an attacker could execute arbitrary SQL queries on your database, or impersonate other users.',
0 commit comments