Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having some problems when i try to create a new file with some content (or overwrite the content of an existing one) using Ubuntu One API and PHP.

I can easily create and empty file or folder using:

PUT /api/file_storage/v1/~/path/to/volume/path/to/node

but i don't understand ho to use this specification:

PUT /api/file_storage/v1/ + <directory.content_path> + '/' + filename.ext, or /api/file_storage/v1/ + <file.content_path>

PUT a new file, with content, in one action, or overwrite content of an existing file.
The body of the PUT request is the content of the file.
Note that you cannot PUT a new file with content and alter its attributes at the same time.
Note also that content_paths may not be rooted under the API root, and may change without warning, so they must be read from the API and not hardcoded.
(Note caveat above about CONTENT_ROOT being temporarily different.)

I don't post the whole code but only the line which doesn't work:

$api_url = '';
$filecontentent = "content of the txt file";

$oauth->fetch($api_url.'~/Ubuntu One.'.$filecontentent.'/try.txt', OAUTH_HTTP_METHOD_PUT);

I don't understand how to structure the syntax. Can you help me?

share|improve this question

The problem you're having is that to put a file with content, you need to get the "content_path" of the directory you want to save the file in, and then PUT the new file under that content_path. See the example code below, which creates a folder ~/Ubuntu One/phptestfolder, gets its content_path, and then PUTs a file foo.txt inside that newly created folder.

# Set up OAuth with the token details you've previously saved
$conskey = 'CCCCCC';
$secret = 'ssssssssssssssssssssssssssssssssssssssssssssssssss';

$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);

# Create a folder in Ubuntu One.
# Folders are created by PUTting to the folder path with a PUT body of
# {"kind": "directory"} as explained at 

$api_url = '';
$oauth->fetch($api_url.'~/Ubuntu%20One/php-test-folder', '{"kind": "directory"}', OAUTH_HTTP_METHOD_PUT);
$response = json_decode($oauth->getLastResponse());

# So now, we want to upload a file to that new folder. To do that, you need
# to get the directory content path. As explained at
# "Note that a directory has a content_path. This means that you can PUT a new 
# file with content into that directory (see below) by PUTting to 
# CONTENT_ROOT + <directory.content-path> + '/' + filename.ext.
# CONTENT_ROOT is the root of the files API itself, /api/file_storage/v1/, but 
# temporarily it should be set to 
# (This note will be removed when this is fixed.)"
# So, we need the directory content path. This is returned in the output from
# the above statement ($oauth->getLastResponse). So, to put a file foo.txt
# with content "this is foo", the URL we need is:
# CONTENT_ROOT:               +
# directory_content_path: $response['content_path']        +
# /: /                                                     +
# filename: foo.txt

# We want to urlencode the path (so that the space in "Ubuntu One", for example,
# becomes %20), but not any slashes therein (so the slashes don't become %2F).
# urlencode() encodes spaces as + so we need rawurlencode
$encpath = rawurlencode($response->content_path);
$encpath = str_replace("%2F", "/", $encpath);

$put_file_url = "" . $encpath . "/" . "foo.txt";
$oauth->fetch($put_file_url, "this is foo", OAUTH_HTTP_METHOD_PUT, array('Content-Type'=>'application/json'));
$response = json_decode($oauth->getLastResponse());

share|improve this answer
up vote 2 down vote accepted

Finally i've found the solution!

There were two problems:

  1. Now as now the url for files content is not the url of the api but

  2. before the file path you should place "content/file path"

Something like this


share|improve this answer
see my answer for more on this: you're not specifically adding "content/" but instead using the content_path from the containing folder. (You shouldn't hardcode that content/ bit because it might change.) – sil Aug 3 '11 at 11:18
I'll use /content for now. I will save it in only one variable so i can easily change it. – Matteo Pagliazzi Aug 13 '11 at 11:25
$oauth->fetch($api_url.'~/Ubuntu One.'.$filecontentent.'/try.txt', OAUTH_HTTP_METHOD_PUT);

I think that there are a few errors:

  • OAUTH_HTTP_METHOD_PUT is the third argument not the second for oauth->fetch()
  • /Ubuntu One.' maybe instead of . you wanted a / ?
  • the content of the file I think it should be the second argument

so, IMHO, the line corrected is:

$oauth->fetch($api_url.'~/Ubuntu One/try.txt', $filecontentent, OAUTH_HTTP_METHOD_PUT);

(maybe you want to corrent even $filecontentent to $filecontent)

share|improve this answer
it doesn't work :( i tried before using code similar to your one... but nothing... – Matteo Pagliazzi Jul 30 '11 at 8:51
try to print error message catching the exception like in the example of code at – fain182 Jul 30 '11 at 9:12
Using this code: try { $oauth->fetch($api_url.'~/Ubuntu One/try.txt', $filecontentent, OAUTH_HTTP_METHOD_PUT); $response_info = $oauth->getLastResponseInfo(); header("Content-Type: {$response_info["content_type"]}"); echo $oauth->getLastResponse(); } catch(OAuthException $E) { echo "Exception caught!\n"; echo "Response: ". $E->lastResponse . "\n"; } i have been redirected to with an error "not found" – Matteo Pagliazzi Jul 30 '11 at 10:00
maybe is a problem with url encoding: $oauth->fetch(urlencode($api_url.'~/Ubuntu One/try.txt'), $filecontentent, OAUTH_HTTP_METHOD_PUT); – fain182 Jul 30 '11 at 10:11
i don't think, i worked with others specification of Ubuntu One Api and all worked. I think there is a problem with the syntax of the url. EDIT: i tried with url encode and now print"Exception caught! Response:" but nothing else – Matteo Pagliazzi Jul 30 '11 at 10:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.