This command adds a file to a folder. It generates a POST
request.
Please note 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¤tFolder=%2F&response_type=txt&langCode=en connector.php?command=FileUpload&type=Files¤tFolder=%2F&CKFinderFuncNum=2&langCode=en connector.php?command=FileUpload&type=Files¤tFolder=%2F&langCode=en
The request will be done using the POST
method (instead of GET
) and the data will be encoded as multipart/form-data
. Only one file per request is sent.
Available Response Structures
The response depends on the 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.
1. Request with 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¤tFolder=%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".
In 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 the fileName|error
. Please note that one of the returned values may be empty.
Some example values:
-
testName.png|
– no errors were 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 (and its name is not available), 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.
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¤tFolder=%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 code with a <script>
tag must be returned. 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. Request with the CKFinderFuncNum
Parameter
Sample request
connector.php?command=FileUpload&type=Files¤tFolder=%2F&CKFinderFuncNum=2&langCode=en
CKFinderFuncNum
is an automatically generated parameter that stores the anonymous callback function reference number (random value) used to pass the URL (fileUrl
) and/or the error message (message
).
Sample response
<script type='text/javascript'>window.parent.CKFINDER.tools.callFunction(funcNum, fileUrl, message)</script>
-
funcNum
– this is theCKFinderFuncNum
parameter that was sent in the 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 just 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>"; ?>