Is it possible to upload avatar from front-end?

I understand users can create profiles from front-end. Awesome!
Now, can they upload avatars? (not Gravatar by the way).

Should be possible using the File Upload class from the toolkit, more info can be found in the docs.

My avatar upload codes:

// User Avatar
if(isset($_FILES['avatar']) && $_FILES['avatar'] != NULL && $_avatar = $_FILES['avatar']['name'])
	$ext = strtolower(pathinfo($_avatar, PATHINFO_EXTENSION));

	$filename = implode('.', array($user->username(), $ext));
	$upload = new Upload(kirby()->roots()->avatars() . DS . $filename, array(
		'input'     => 'avatar',
		'overwrite' => true

	if($file = $upload->file()) 
		$image = new ImageConverter($file, array(
			'tosRGB' => true,
			'width' => 1024,
			'height' => 1024,
		messages()->add($upload->error(), 'error');
1 Like

Makes sense!
You are assuming the user is aready logged in, right?

@texnixe Docs outline generic uploads, but not avatars, so avatars are not special?

No, you can upload avatars just like other images; the only thing to keep in mind is to upload them to the right folder (/assets/avatars) and to match user name and avatar file name. The panel uses the same upload class to upload user avatars.

1 Like

Let’s see:

$upload = new Upload(__DIR__ . DS . 'assets' . DS . 'avatars' . DS . $file_name , array(
  'input' => 'avatar',
  'overwrite' => true

The picture is there:

dump($_FILES["avatar"]["name"]) => picture.png
dump($_FILES["avatar"]["error"]) => 0 (zero means no error, right?)

Upload function throws error:

dump($upload->error()) => The file could not be moved…

Any ideas?

By the way I’m trying to let the user upload avatar right from registration form.
The registration form is already working fine.
Not sure if user needs to be authenticated to upload files.

Yes, sure! Users checking on routes like that:

$routes['my-user-info'] = array(
	'pattern' => 'my-account/user-info',
	'method' => 'GET|POST',
	'action'  => function()
			return site()->visit(page('my-account/user-info'));
		} go(urlto('login').'?redirect='.urito('my-user-info', null, false));

__DIR__ points to the directory of the current script, so that depends on where you create the Upload instance and is not necessarily equal to the root of the site. Try the following:

$upload = new Upload(kirby()->roots->avatars() . DS . $file_name, array(
  'input' => 'avatar',
  'overwrite' => true
1 Like


@lukasbestle I expected the docs example to work whatever the path to give me a rough idea. But it didn’t.

I tried: __DIR__ . DS . 'whatever'
=> Error: “The file could not be moved”

I tried: kirby()->roots->avatars()
=> Success!

What’s the difference?

I didn’t know about kirby()->roots->avatars()
Is it documented?

For __DIR__ . DS . 'whatever' to work, the directory whatever needs to exist in the same directory as the file that uses it already. The avatar directory does exist, so that works out of the box.

The examples of the Toolkit are general and not specific to Kirby because you can use the Toolkit without the CMS.

The $roots object is documented, but the avatars() method seems to be missing. I will add it later today, thanks for reporting.

1 Like

So, directory needs to exist, bang! I think that’s the whole thing.
Anyway, you guys are awesome!