FileUpload

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.

Note: This information is intented for programmers interested in modifying the CKFinder core.
If you are interested in installing/configuring CFKinder, please take a look at the Developer's Guide.

FileUpload

Adds a file in a folder. It generates a POST

It is also worth noting that for all three methods mentioned below uploaded file names may contain non ASCII characters.

Sample Request

Upload a file to the root directory of the Files resource type.

connector.php?command=FileUpload&type=Files&currentFolder=%2F&response_type=txt&langCode=en

connector.php?command=FileUpload&type=Files&currentFolder=%2F&CKFinderFuncNum=2&langCode=en

connector.php?command=FileUpload&type=Files&currentFolder=%2F&langCode=en

The request will be done using POST method (instead of GET) and the data will be encoded as multipart/form-data. Only one file per request is sent.

Sample Response

The response depends on upload method used and parameters that were sent in the request. There are three types of responses that can be sent from the server to the client.

Available Response Structures:

1. A request contains the response_type=txt parameter

This is the response method used by CKFinder for HTML5 and Flash uploads. It was introduced in CKFinder 2.1. Anyone using a custom server-side connector should adjust the code to use this method.

Sample request:

connector.php?command=FileUpload&type=Files&currentFolder=%2F&response_type=txt&langCode=en

Sample response:

testName.png|
|Invalid file. The file size is too big.
testName(1).png|A file with the same name is already available. The uploaded file was renamed to "testName(1).png".

With this approach the connector has to return a plain text response (content type should be text/plain): the file name followed by a pipe character and the error message in the format of error. Please note that one of the returned values may be empty.

Some example values:

  • testName.png| – no errors found during the upload process. The response consists of the file name and the pipe character with no error message to follow.
  • |Invalid file. The file size is too big. – the file was not uploaded (we do not have its name), but the error was reported and can be returned to the client.
  • testName(1).png| A file with the same name is already available. The uploaded file was renamed to "testName(1).png – the file was uploaded, but an unexpected condition has occurred so the error message is returned as well.


Thanks to the langCode parameter sent in the request you can use localized messages instead of having them in English.

Failed upload process for HTML5
Failed upload process for Flash


2. Response Structure for Simple HTML Uploads

This method is used for old browsers that do not support HTML5, where Flash cannot be installed for some reason and the only way to upload a file is to use the old classic HTML4 upload method.

Sample request:

connector.php?command=FileUpload&type=Files&currentFolder=%2F&langCode=en

Sample response:

<script type="text/javascript">
     window.parent.OnUploadCompleted( 0, '' ) ;
</script>

This command does not expect XML to be returned and actually HTML must return with a <script> tag. The content type must be text/html. The OnUploadCompleted is a JavaScript function that is called to expose the upload result.

Some example values:

  • OnUploadCompleted( 0 ) – no errors found during the upload process.
  • OnUploadCompleted( 1, 'Reason' ) – the upload failed because of the reason given.
  • OnUploadCompleted( 201, 'FileName(1).ext' ) – the file was uploaded successfully, but its name was changed to "FileName(1).ext".
  • OnUploadCompleted( 202 ) – invalid file.


Thanks to the langCode parameter in the request you can use localized messages instead of having them in English. Note that if this method is used, the content type must be text/html.

3. The Request Contains the CKFinderFuncNum Parameter

important note

This method is currently not being used by core CKFinder. It was introduced in version 2.0 in order to allow custom dialog windows created for CKFinder for uploading files (Please note that such dialog windows should use file elements - please see


Sample request:

connector.php?command=FileUpload&type=Files&currentFolder=%2F&CKFinderFuncNum=2&langCode=en

CKFinderFuncNum – It is an automatically generated parameter that stores anonymous callback function reference number (random value) used to pass the URL (fileUrl) and/or error message (message).

Sample response:

<script type='text/javascript'>window.parent.CKFINDER.tools.callFunction(funcNum, fileUrl, message)</script>
  • funcNum – this is the CKFinderFuncNum parameter that was sent in a request.
  • fileUrl – the URL to the uploaded file (optional). If the file was not uploaded, this argument remains empty.
  • message – the error message (optional).


There are no special error numbers given in the callback function. Your script can return a URL to use and a message to show to the user — both are optional, so you can just return a URL that will be used in the dialog window, or you can return a message that will be shown to the user, or a URL and a message (for example a warning that the file was renamed along with the new URL).

Thanks to the langCode parameter in the request you can use localized messages instead of having them in English. Note that if this method is used, the content type must be text/html.

The following code shows how to send back the URL of the uploaded file from the PHP connector:

<?php
// Required: anonymous function reference number as explained above.
$funcNum = $_GET['CKFinderFuncNum'] ;
// Optional: might be used to provide localized messages.
$langCode = $_GET['langCode'] ;
 
// Check the $_FILES array and save the file. Assign the correct path to a variable ($url).
$url = '/path/to/uploaded/file.ext';
// Usually you will only assign something here if the file could not be uploaded.
$message = '';
 
echo "<script type='text/javascript'>window.parent.CKFINDER.tools.callFunction($funcNum, '$url', '$message');</script>";
?>