baseDir and baseURL Parameters Explained

This website contains links to software which is either no longer maintained or will be supported only until the end of 2019 (CKFinder 2). For the latest documentation about current CKSource projects, including software like CKEditor 4/CKEditor 5, CKFinder 3, Cloud Services, Letters, Accessibility Checker, please visit the new documentation website.

If you look for an information about very old versions of CKEditor, FCKeditor and CKFinder check also the CKEditor forum, which was closed in 2015. If not, please head to StackOverflow for support.

(Minor text restructuring)
 
(7 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
Both <code>baseDir</code> and <code>baseURL</code> should point to the same location on the server &mdash; the <code>userfiles</code> directory that contains all user files uploaded with CKFinder. The difference between them is as follows:
 
Both <code>baseDir</code> and <code>baseURL</code> should point to the same location on the server &mdash; the <code>userfiles</code> directory that contains all user files uploaded with CKFinder. The difference between them is as follows:
 
* <code>baseURL</code> gives a full URL to the <code>userfiles</code> directory or a path that is ''relative'' to the domain. For example:
 
* <code>baseURL</code> gives a full URL to the <code>userfiles</code> directory or a path that is ''relative'' to the domain. For example:
*; <source lang="xml"><baseURL>http://example.com/CKFinderJava-X.Y/userfiles/</baseURL></source>
+
*; <source lang="xml"><baseURL>http://example.com/CKFinderJava/userfiles/</baseURL></source>
*; <source lang="xml"><baseURL>/CKFinderJava-X.Y/userfiles/</baseURL></source>
+
*; <source lang="xml"><baseURL>/CKFinderJava/userfiles/</baseURL></source>
 
* <code>baseDir</code> gives an ''absolute'' path to the directory on the server (a physical machine). For example:
 
* <code>baseDir</code> gives an ''absolute'' path to the directory on the server (a physical machine). For example:
*; <source lang="xml"><baseDir>/usr/tomcat/webapps/CKFinderJava-X.Y/userfiles/</baseDir></source>
+
*; <source lang="xml"><baseDir>/usr/tomcat/webapps/CKFinderJava/userfiles/</baseDir></source>
*; <source lang="xml"><baseDir>C:\tomcat\webapps\CKFinderJava-X.Y\userfiles\</baseDir></source>
+
*; <source lang="xml"><baseDir>C:\tomcat\webapps\CKFinderJava\userfiles\</baseDir></source>
 
 
<note>Note that <code>''X.Y''</code> in the code above denotes a CKFinder version, like <code>2.0</code>.
 
</note>
 
 
 
  
 
You may ask: why use two paths to point to one location?
 
You may ask: why use two paths to point to one location?
Line 26: Line 22:
 
<!-- This configuration is wrong. -->
 
<!-- This configuration is wrong. -->
 
<baseDir>/userfiles/</baseDir>
 
<baseDir>/userfiles/</baseDir>
<baseURL>/CKFinderJava-X.Y/userfiles/</baseURL>
+
<baseURL>/CKFinderJava/userfiles/</baseURL>
 
</source>
 
</source>
  
Line 35: Line 31:
 
<!-- This configuration is wrong. -->
 
<!-- This configuration is wrong. -->
 
<baseDir>C:\userfiles</baseDir>
 
<baseDir>C:\userfiles</baseDir>
<baseURL>/CKFinderJava-X.Y/userfiles/</baseURL>
+
<baseURL>/CKFinderJava/userfiles/</baseURL>
 
</source>
 
</source>
  
Line 47: Line 43:
 
As mentioned above, CKFinder works on files and for this it uses the <code>baseDir</code> parameter value. All uploaded files will thus be placed in the location that is set in this parameter. HTTP clients, on the other hand, will use the <code>baseURL</code> value, but in both examples presented above this parameter will point to a non-existent location which in some cases may lead to a lack of access to files.
 
As mentioned above, CKFinder works on files and for this it uses the <code>baseDir</code> parameter value. All uploaded files will thus be placed in the location that is set in this parameter. HTTP clients, on the other hand, will use the <code>baseURL</code> value, but in both examples presented above this parameter will point to a non-existent location which in some cases may lead to a lack of access to files.
  
== More on baseURL ==
+
== BaseDir and BaseUrl best practice ==
As stated above, if the <code>baseDir</code> parameter is empty, CKFinder will try to deduce the <code>userfile</code> path based on the <code>baseURL</code> parameter value.
+
<note>It is always recommended to set <strong>both</strong> <code>baseUrl</code> and <code>baseDir</code> so that they point to same location which is outside of an application directory.</note>
 +
 
 +
 
 +
In previous section, we have seen what problems we may get when these two properties are misconfigured but what will happen when only <code>baseUrl</code> is set? CKFinder needs both, <code>baseUrl</code> and <code>baseDir</code> to work correctly and if <code>baseDir</code> is missing, CKFinder will try to determine its value based on the <code>baseUrl</code> property.  You must know however that CKFinder has limited capabilities in this case. To be more precise, CKFinder is mainly limited to application context (application main directory).  
 +
Let’s have a look at few examples:
 
<source lang="xml">
 
<source lang="xml">
 
<baseURL>/userfiles/</baseURL>
 
<baseURL>/userfiles/</baseURL>
 +
<baseDir></baseDir>
 +
<!--or-->
 +
<baseURL>http://example.com/userfiles/</baseURL>
 
<baseDir></baseDir>
 
<baseDir></baseDir>
 
</source>
 
</source>
  
If the server hosts more applications for one domain (like <code>localhost</code> or <code>www.example.com</code>), the code above will not work for most of them. To be exact, it will work only for the application that is set as the default one for the domain.
+
In both above cases the <code>userfiles</code> folder points to server’s main applications directory. When multiple applications are kept on server they are accessible like <code><nowiki>http://example.com/CKFinderJava-2.5</nowiki></code> or <code><nowiki>http://example.com/CKFinderJava-2.4</nowiki></code>. As you can probably see the <code>userfiles</code> folder should be created on same level as applications in order to be accessible through HTTP. The problem is that applications folder is not accessible from the web. There are however two servers which have general accessible folders and to which CKFinder can get. These servers are Apache Tomcat and Oracle GlassFish. Having configuration like presented above, <strong>starting from CKFinder 2.5.1</strong>,  CKFinder will create <code>userfiles</code> folder in <code>TOMCAT_HOME/webapps/ROOT</code> directory for Apache Tomcat and <code>GLASSFISH_HOME/glassfish/domains/domain_name/docroot </code> directory for Oracle GlassFish. Everything will work as expected in this case. For other supported servers, CKFinder will use fallback approach and create <code>userfiles</code> folder inside <code>CKFinderJava</code> application directory. As you probably know, this will unfortunately result in <strong>View</strong> option not working properly.
  
This may sound a bit enigmatic, but here is the explanation.
+
Let’s take a look at another example:
 +
<source lang="xml">
 +
<baseURL>/CKFinderJava/userfiles/</baseURL>
 +
<baseDir></baseDir>
 +
</source>
  
If the server hosts multiple applications in one domain, the users usually call them in the following way (note this does not pertain to the default application):
+
Above setting will create <code>userfiles</code> inside <code>CKFinderJava</code> application directory. Although such configuration will work on every server there is a caveat to that approach. It isn’t recommended to put folder for uploaded files into deployed application folder because with every redeployment (e.g. when upgrading <code>war</code> to new version) of the application, the upload folder will be removed.
 +
<strong>So, what should be done to set <code>baseUrl</code> and <code>baseDir</code> correctly? </strong>
  
<code><nowiki>http://localhost:8080/CKFinderJava-X.Y/</nowiki><br />
+
<note>
<nowiki>http://www.example.com/CKFinderJava-X.Y/</nowiki></code>
+
First of all you should set <strong>both</strong> properties so that they <strong>point to same location</strong> which is <strong>accessible</strong> through <code><strong>HTTP</strong></code> and from <code> <strong>the file system</strong></code>. Second, you should <strong>set these properties to point to location outside of CKFinder application folder</strong>.</note>
  
or the following way, where <code>filemanager</code> is the ''context path'' for the <code>CKFinderJava-''X.Y''</code> application:
+
One solution is using [https://en.wikipedia.org/wiki/Symbolic_link symlinks]. The <code>userfiles</code> folder located inside application context can be a symbolic link to some other directory on the disk. With that simple approach <code>/CKFinderJava/userfiles/</code> can point to folder of your choice. One downside of this technique is that you have to recreate the symbolic link with every application redeploy as the symlink gets removed on redeployment.
  
<code><nowiki>http://localhost:8080/filemanager/</nowiki><br />
+
Another solution is using server features. Most of the servers provide "virtual directory" mechanism. A "Virtual Directory" allows accessing through <code><strong>HTTP</strong></code> a folder which can be located even outside of the server. In Tomcat 7-8 this can be done through <strong>aliases</strong>, in Tomcat 6 by <strong>adding another context to <code>server.xml</code></strong>, in GlassFish through <strong>alternate docroots</strong>, in Jetty by <strong>adding another context to <code>jetty.xml</code></strong>, in Weblogic through <strong>virtual directories</strong>, in WildFly by <strong>adding extra entries to <code>urn:jboss:domain:undertow</code> <code>subsystem</code> element</strong> in one of configuration files e.g. standalone.xml  and in JBOSS EAP by <strong>allowing symlinks</strong> for particular external folder in <code>jboss-web.xml</code> file and then <strong>making that symlink</strong>.<br />
<nowiki>http://www.example.com/filemanager/</nowiki></code>
 
  
These applications are (physically) located in one directory (like <code>webapps</code> for Tomcat or Jetty) and to differentiate between them, the user needs to include the name or alias (context path) of the called application in its URL.
+
Let’s have a look at an example for Apache Tomcat 8. The server is installed in <code>C:/Apache-Tomcat-8/</code> directory, CKFinderJava is installed in <code>C:/Apache-Tomcat-8/CKFinderJava</code>, we want to have our <code>userfiles</code> folder located directly on <code>C</code> drive and we want that folder to be called <code>"myimages"</code>. Here are the settings that will allow having all CKFinder features working, and <code>userfiles</code> folder not being removed with every redeployment of the application:
When the <code>baseURL</code> parameter is set to point directly to the <code>userfiles</code> folder, CKFinder will create this folder in the application directory (like <code> CKFinderJava-''X.Y''/userfiles/</code>). On the other hand, when the user will want to use the '''View''' option, the browser will search for the image in the <code><nowiki>http://localhost:8080/userfiles/</nowiki></code> directory (<code><nowiki>http://hostName/baseURL/</nowiki></code>) instead of <code><nowiki>http://localhost:8080/CKFinderJava-X.Y/userfiles/</nowiki></code>.
 
  
The first address points to the <code>userfiles</code> directory placed in the root of the web applications directory. Since the CKFinder <code>userfiles</code> directory is located elsewhere, the image will not be found and the browser will display the "404 &mdash; Not found" error.
+
CKFinder config.xml settings:
 +
<source lang="xml">
 +
<baseDir>C:/myimages</baseDir>
 +
<baseURL>/CKFinderJava/userfiles/</baseURL>
 +
</source>
  
It is worth noting that the example above will work if you set one of the applications as a default for a domain (for example by using virtual hosting). Do remember, though, that this is only possible for one application per domain. With this configuration in place the <code>userfiles</code> directory will be created in the application root, but by default the application root is associated with the domain name, so calling <code><nowiki>http://localhost:8080/userfiles/</nowiki></code> will let you search in the <code>CKFinderJava-''X.Y''</code> application folder and display the image.
+
CKFinderJava/META-INF/context.xaml settings:
 +
<source lang="xml">
 +
<Context antiJARLocking="true" path="/CKFinderJava" reloadable="true" >
 +
    <Resources>
 +
      <PreResources base="C://myimages" className="org.apache.catalina.webresources.DirResourceSet" webAppMount="/userfiles" />
 +
  </Resources>
 +
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"  allow=".*" />
 +
</Context>
 +
</source>
 +
With the help of the server and two configuration changes, you can have your files being saved to <code>C:/myimages</code> folder. It’s also worth mentioning that <code>myimages</code> directory is invisible to end user and Tomcat serves files as if they really were located inside <code> CKFinderJava/userfiles/</code>.

Latest revision as of 21:54, 29 November 2015

Both baseDir and baseURL should point to the same location on the server — the userfiles directory that contains all user files uploaded with CKFinder. The difference between them is as follows:

  • baseURL gives a full URL to the userfiles directory or a path that is relative to the domain. For example:
    <baseURL>http://example.com/CKFinderJava/userfiles/</baseURL>
    <baseURL>/CKFinderJava/userfiles/</baseURL>
  • baseDir gives an absolute path to the directory on the server (a physical machine). For example:
    <baseDir>/usr/tomcat/webapps/CKFinderJava/userfiles/</baseDir>
    <baseDir>C:\tomcat\webapps\CKFinderJava\userfiles\</baseDir>

You may ask: why use two paths to point to one location?

The baseURL parameter represents the path that is used by HTTP clients. It is employed for example when CKFinder returns the URL addresses of the files.

CKFinder itself is not an HTTP client, but an application that operates on server-based files that needs absolute paths to these files to operate correctly. A specific server configuration may sometimes lead to problems with setting the absolute path based on the baseURL parameter, which leads to the malfunctioning of the application. This is where the baseDir parameter that contains the direct path to the userfiles directory may help.

Misconfiguration of baseURL and baseDir

Since setting the parameters may sometimes seem counter-intuitive, especially for the beginners, here are a couple of examples of CKFinder misconfiguration, i.e. the situation where the baseURL and baseDir settings were wrong.

Example 1: Using a Relative Path for baseDir

<!-- This configuration is wrong. -->
<baseDir>/userfiles/</baseDir>
<baseURL>/CKFinderJava/userfiles/</baseURL>

In this example the baseDir parameter contains the relative (and not absolute) path to the userfiles directory. In a Windows system with this configuration the userfiles directory will be created on the same partition that hosts the server (for example, C:\userfiles). On a Linux machine the system will try to create a /userfiles/ directory which will probably fail due to missing server permissions to create new directories in /.

Example 2: Incoherent Directory Paths

<!-- This configuration is wrong. -->
<baseDir>C:\userfiles</baseDir>
<baseURL>/CKFinderJava/userfiles/</baseURL>

In this example baseDir is set to an absolute path, but the parameter points to a different location than baseURL that is set relative to the application root.

Result: CKFinder Misconfiguration

In both examples the parameters point to different server directories. As a result, some CKFinder features may not work correctly. The View function, for example, will be one of them.

Why is that so?

As mentioned above, CKFinder works on files and for this it uses the baseDir parameter value. All uploaded files will thus be placed in the location that is set in this parameter. HTTP clients, on the other hand, will use the baseURL value, but in both examples presented above this parameter will point to a non-existent location which in some cases may lead to a lack of access to files.

BaseDir and BaseUrl best practice

important note
It is always recommended to set both baseUrl and baseDir so that they point to same location which is outside of an application directory.


In previous section, we have seen what problems we may get when these two properties are misconfigured but what will happen when only baseUrl is set? CKFinder needs both, baseUrl and baseDir to work correctly and if baseDir is missing, CKFinder will try to determine its value based on the baseUrl property. You must know however that CKFinder has limited capabilities in this case. To be more precise, CKFinder is mainly limited to application context (application main directory). Let’s have a look at few examples:

<baseURL>/userfiles/</baseURL>
<baseDir></baseDir>
<!--or-->
<baseURL>http://example.com/userfiles/</baseURL>
<baseDir></baseDir>

In both above cases the userfiles folder points to server’s main applications directory. When multiple applications are kept on server they are accessible like http://example.com/CKFinderJava-2.5 or http://example.com/CKFinderJava-2.4. As you can probably see the userfiles folder should be created on same level as applications in order to be accessible through HTTP. The problem is that applications folder is not accessible from the web. There are however two servers which have general accessible folders and to which CKFinder can get. These servers are Apache Tomcat and Oracle GlassFish. Having configuration like presented above, starting from CKFinder 2.5.1, CKFinder will create userfiles folder in TOMCAT_HOME/webapps/ROOT directory for Apache Tomcat and GLASSFISH_HOME/glassfish/domains/domain_name/docroot directory for Oracle GlassFish. Everything will work as expected in this case. For other supported servers, CKFinder will use fallback approach and create userfiles folder inside CKFinderJava application directory. As you probably know, this will unfortunately result in View option not working properly.

Let’s take a look at another example:

<baseURL>/CKFinderJava/userfiles/</baseURL>
<baseDir></baseDir>

Above setting will create userfiles inside CKFinderJava application directory. Although such configuration will work on every server there is a caveat to that approach. It isn’t recommended to put folder for uploaded files into deployed application folder because with every redeployment (e.g. when upgrading war to new version) of the application, the upload folder will be removed. So, what should be done to set baseUrl and baseDir correctly?

important note
First of all you should set both properties so that they point to same location which is accessible through HTTP and from the file system. Second, you should set these properties to point to location outside of CKFinder application folder.

One solution is using symlinks. The userfiles folder located inside application context can be a symbolic link to some other directory on the disk. With that simple approach /CKFinderJava/userfiles/ can point to folder of your choice. One downside of this technique is that you have to recreate the symbolic link with every application redeploy as the symlink gets removed on redeployment.

Another solution is using server features. Most of the servers provide "virtual directory" mechanism. A "Virtual Directory" allows accessing through HTTP a folder which can be located even outside of the server. In Tomcat 7-8 this can be done through aliases, in Tomcat 6 by adding another context to server.xml, in GlassFish through alternate docroots, in Jetty by adding another context to jetty.xml, in Weblogic through virtual directories, in WildFly by adding extra entries to urn:jboss:domain:undertow subsystem element in one of configuration files e.g. standalone.xml and in JBOSS EAP by allowing symlinks for particular external folder in jboss-web.xml file and then making that symlink.

Let’s have a look at an example for Apache Tomcat 8. The server is installed in C:/Apache-Tomcat-8/ directory, CKFinderJava is installed in C:/Apache-Tomcat-8/CKFinderJava, we want to have our userfiles folder located directly on C drive and we want that folder to be called "myimages". Here are the settings that will allow having all CKFinder features working, and userfiles folder not being removed with every redeployment of the application:

CKFinder config.xml settings:

<baseDir>C:/myimages</baseDir>
<baseURL>/CKFinderJava/userfiles/</baseURL>

CKFinderJava/META-INF/context.xaml settings:

<Context antiJARLocking="true" path="/CKFinderJava" reloadable="true" >
    <Resources>
       <PreResources base="C://myimages" className="org.apache.catalina.webresources.DirResourceSet" webAppMount="/userfiles" />
   </Resources>
   <Valve className="org.apache.catalina.valves.RemoteAddrValve"  allow=".*" />
</Context>

With the help of the server and two configuration changes, you can have your files being saved to C:/myimages folder. It’s also worth mentioning that myimages directory is invisible to end user and Tomcat serves files as if they really were located inside CKFinderJava/userfiles/.

This page was last edited on 29 November 2015, at 21:54.