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.

Line 43: 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 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 <code>baseUrl</code>. 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> <br />
 +
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>. <br />
 +
<strong>Second, you should set these properties to location outside of CKFinder application folder</strong>. Here is where server itself might be helpful as most of the servers provide “virtual directories”. A “Virtual Directory” allows accessing folder which can be located even outside of the server, through <code><strong>HTTP</strong></code>. 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> and In Weblogic through <strong>virtual directories</strong>. <br />
  
<code><nowiki>http://localhost:8080/CKFinderJava/</nowiki><br />
+
Let’s have a look at an example for Tomcat 8 server. The server is installed in <code>C:/Apache-Tomcat-8/</code> folder, 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:
<nowiki>http://www.example.com/CKFinderJava/</nowiki></code>
 
  
or the following way, where <code>filemanager</code> is the ''context path'' for the <code>CKFinderJava</code> application:
+
CKFinder config.xml settings:
 +
<source lang="xml">
 +
<baseDir>C:/myimages</baseDir>
 +
<baseURL>/CKFinderJava/userfiles/</baseURL>
 +
</source>
  
<code><nowiki>http://localhost:8080/filemanager/</nowiki><br />
+
CKFinderJava/META-INF/context.xaml settings:
<nowiki>http://www.example.com/filemanager/</nowiki></code>
+
<source lang="xml">
 
+
<Context antiJARLocking="true" path="/CKFinderJava" reloadable="true" >
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.
+
    <Resources>
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/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/userfiles/</nowiki></code>.  
+
      <PreResources base="C://myimages" className="org.apache.catalina.webresources.DirResourceSet" webAppMount="/userfiles" />
 
+
  </Resources>
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.
+
  <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow=".*" />
 
+
</Context>
It is worth mentioning 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</code> application folder and display the image.
+
</source>

Revision as of 22:45, 3 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 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 baseUrl. 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 CKFinderJavaapplication 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?
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 location outside of CKFinder application folder. Here is where server itself might be helpful as most of the servers provide “virtual directories”. A “Virtual Directory” allows accessing folder which can be located even outside of the server, through HTTP. 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 and In Weblogic through virtual directories.

Let’s have a look at an example for Tomcat 8 server. The server is installed in C:/Apache-Tomcat-8/ folder, 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>