How to store uploaded files in separate folders for each user

KTML 4 uploads media, documents and templates files to the default folder which is automatically created inside your site root folder. You can change this folder when adding and configuring the KTML 4 control to use any folder in your site root. However, paths to the media, documents and templates folders can be determined dynamically, based on a desired variable. This allows you to have a different upload folder for each logged in user - for example - but still use a single page with the KTML editor applied upon. This case will be explained in this tutorial.

Scenario and requirements

Suppose you have a database-driven site where users can login and post articles through a KTML enhanced page. When the user logs in, a session variable named kt_login_user is created, storing the logged in user name. You want to allow each user to save the files he uploads through KTML in the <site_root>\uploads\[username]\ folder. Also, the article posting page cannot be accessed by non-logged in users.

Before following this tutorial, it is assumed that you already have:

 

If you login and use the editor with the default KTML options, and upload an image or file, it will be stored in <site_root>/uploads. To make the folders dynamic, you will have to modify the page code, where the KTML properties are set. You have to append the value of the session variable storing the user name to the statically defined uploads folder.

 

In the next topic you will learn how to change the code in order to use dynamic folder names, for each of the supported server models.