0Day Forums
OctoberCMS frontend image upload - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: CMS (https://zeroday.vip/Forum-CMS)
+--- Thread: OctoberCMS frontend image upload (/Thread-OctoberCMS-frontend-image-upload)



OctoberCMS frontend image upload - calamopitysmbe - 07-19-2023

I'm trying to upload profile phoho in front end of OctoberCMS but with no luck.

Cannot catch it in the backend to save within the user.

Made everything exactly like in documentation of the CMS.

Need to make this work without any eternal plugins.


<form data-request="onCreateUser" data-request-update="user:'.res_user'" >

<input name="file_input" type="file" >
<button type="submit" class="subBtn">Create User</button>

</form>

In User i have:

public $attachOne = [
'avatar' => 'System\Models\File'
];

and this code:

$file = new System\Models\File;
$file->data = Input::file('file_input');
$file->is_public = true;
$file->save();

$model->avatar()->add($file);

Record stored in DB, but its empty. filesize = 0

Can anyone please help me with this?
Asked the big Bro, but found that this problem is very common, and have no solution.

Thanks


Update:

This is working, when I use this:

{{ form_open({files: true, request: 'onCreateConcert'}) }}
<!--File Input-->
<input type="file" name="file-upload" required="required">
<!--File Input-->
<!--Submit/Upload Button-->
<button type="submit">Upload</button>
{{ form_close() }}

but not with my form. whats i'm missing here?

I added as well token and session_key

{{ form_sessionKey() }}
{{ form_token() }}


RE: OctoberCMS frontend image upload - haddie841260 - 07-19-2023

try this:

use System\Models\File;
use Input;

[...]

if (Input::file("file_input")) {
$model->avatar = Input::file("file_input");
}
model->save();



RE: OctoberCMS frontend image upload - dilyskyngagj - 07-19-2023

It because you don't add `enctype="multipart/form-data"` to your form attribute.

When you use twig like `{{ form_open({files: true, request: 'onCreateConcert'}) }}`, the `files: true` will add attribute to your form to handle upload file. So the twig above will generate tag like below,

```
<form data-request="onCreateConcert" enctype="multipart/form-data">
```