Upload files: Include username in folder path

How to create user-specific directories for file uploads in ChronoForms.

Overview

The issue occurs because the upload path with a dynamic placeholder for the username does not automatically create the required directory on the server.
Use a custom PHP action before the upload to check for and create the directory based on the user ID, then reference the created directory path in the upload settings.

Answered
ChronoForms v6
pa pat01 26 Oct, 2019
Hi

I use upload field with upload path:
/home/public_html/dev/images/members/

This works fine.

Now I want CF to add a directory for each user uplaoding files:
/home/public_html/dev/images/members/{user:username}

But this is not working. I get
Destination directory not available.

How can I make CF to create such directories?

Thanks.

Patrick

CF 6.1.2
Gr GreyHead 26 Oct, 2019
HI Patrick.

It should work OK I think. Please check the permissions for the /members/ folder and check that the Joomla user has write permission there. That is the most likely cause.

Do you see anything useful in the Debugger output?

Bob
pa pat01 27 Oct, 2019
Hello Bob

CF does give the path including user directory:

Upload files: Include username in folder path image 1



Username is "test1".

But directory is not created on server. Directory "zertifizierung" has chmod 777.


and check that the Joomla user has write permission there
How can I do this? It's not only one user uploading, there are many users supposed to upload files.

Thanks.

Patrick.
Gr GreyHead 27 Oct, 2019
Hi Patrick,

The permission question isn't about the form users - it's a question about the server setup. Can Joomla! create new folders . . . you may need to check with your host for the exact settings.

Bob
he healyhatman 27 Oct, 2019
1 Likes
Pretty sure you will need to use PHP to create the folder first, and since people can use certain special characters in usernames it might be a bad idea - user ID would be better
Gr GreyHead 27 Oct, 2019
1 Likes
Hi Patrick,

There is a FAQ here with the code for CFv4 and CFv5 - it should be possible to adapt the CFv5 code for CFv6.

Bob
pa pat01 28 Oct, 2019
Answer
@healyhatman
Thanks for the hint. I'll use user ID.

@Bob
I can install extensions without any problems. There shouldn't be permission problems.
Thanks for the link to the code. This gave me a good start.

I've put together following code and execute it before the upload action:
<?php
$jid = JFactory::getUser()->id;
$directoryPath = $_SERVER['DOCUMENT_ROOT'] . "/images/mitglieder/zertifizierung/";

if($jid > 0) {
$directoryName = $jid;
}
else {
$directoryName = "guests";
}

if(!is_dir($directoryPath.$directoryName)){
mkdir($directoryPath.$directoryName, 0755);
}

return $this->data("userdir", $directoryName, true);
?>

And my setting in Upload directory path looks like this:
{path:root}/images/mitglieder/zertifizierung/{data:userdir}


This works fine.

Patrick
This topic is locked and no more replies can be posted.