Upload a document
upload
POST https://collaboractor.com/api/v1/upload?login=&password=
login | Your identification code. |
---|---|
password | Your password. |
multipart/form-data | |
file | Content of the document in binary. |
$ curl -D - -X POST "https://collaboractor.com/api/v1/upload?login=abcdef&password=ABCDEF" -F "file=@file_sample_500kB.doc;type=application/msword"
NOTE: Use a command such as file -ib file_sample_500kB.doc
to obtain the MIME type of the file passed with the argument file
.
If the file name is invalid or if the type of the file isn't supported or if the size of the file is greater than the maximum size of a file in your personal space, the service returns the error HTTP/1.1 400 Bad Request
.
if the number of files in your personal space has reached the authorized limit, the service returns the error HTTP/1.1 403 Forbidden
.
If a file with the same name already exists in your personal space, the service returns the error HTTP/1.1 409 Conflict
.
Download the code of the sendpost
and file_mime_type
functions from the iZend library.
Copy the files in the space of your application.
NOTE: See the page Call the service API for a description of the sendpost
and file_mime_type
functions.
Add the file fileupload.php with the following content:
- require_once 'sendhttp.php';
- require_once 'filemimetype.php';
Loads the code of the sendpost
and file_mime_type
functions.
- function file_upload($login, $password, $file) {
Defines the function file_upload
.
$login
is your identification code. $password
is your password.
$file
is the pathname of the document to upload.
- $curl = 'https://collaboractor.com/api/v1/upload' . '?' . 'login=' . urlencode($login) . '&' . 'password=' . urlencode($password);
Sets $curl
to the URL of the upload action with the identification code and the password of the user's account.
$login
and $password
must be escaped.
- $files=array('file' => array('name' => basename($file), 'tmp_name' => $file, 'type' => file_mime_type($file)));
Prepares the list of files attached to the POST: file
- the document with the name of the file, the pathname of the file and its MIME type.
- $response=sendpost($curl, false, $files);
Sends the HTTP request with sendpost
.
The arguments login
and password
are already in $curl
.
- if (!$response or $response[0] != 200) {
- return false;
- }
If $response
is false
, the server is unreachable.
If $response[0]
doesn't contain the HTTP return code 200 Ok, an execution error has occurred.
In case of error, file_upload
returns false.
- return true;
- }
Returns true
if the document has been added into your personal space.
EXAMPLE
Assuming you have saved the files sendhttp.php, filemimetype.php and fileupload.php in the current directory, run PHP in interactive mode, load the file_upload
function and call it with as arguments your identification code and password, the pathname of document managed by LibreOffice:
$ php -a
php > require_once 'fileupload.php';
php > echo file_upload('abcdef', 'ABCDEF', 'file_sample_500kB.doc') ? 'Ok' : 'Ko';
Ok
php > quit
NOTE: If file_upload
returns false
, edit the code to display $response
.
SEE ALSO
Call the service API, Edit a document, Download a document, Delete a document
Comments