We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.
Forum Discussion
Stefano B.3
9 years agoNew member | Level 1
Upload file directly on dropbox
HI ,sorry for english, Im learning about upload a file on server to dropbox .
[code]
<?php
require_once "dropbox-sdk/Dropbox/autoload.php";
use \Dropbox as dbx;
$accessToken='My_access_token ';
$dbxClient = new dbx\Client($accessToken, "MyApp/1.0");
$f = fopen("prova.txt", "rb");
$result = $dbxClient->uploadFile("/cambio_nome.txt", dbx\WriteMode::add(), $f);
fclose($f);
print_r($result);
[/code]
First question :
The response is an array , how can i know i the response is success ?
Second question :
Can i upload file directly on dropbox without upload on my server ?
Third question :
Can i control if a folder exist in my app and create it if not exist than insert file in this folder
- Greg-DBDropbox Staff
[Cross-linking for reference: https://stackoverflow.com/questions/38935509/dropbox-upload-file ]
It looks like you're using the PHP Core SDK for the deprecated API v1. We recommend migrating to API v2 whenever possible. We don't have an official PHP SDK for API v2, but there are some third party ones listed here.
Anyway, to answer your questions:
1) That uploadFile method will return the metadata for the uploaded file if the upload was successful, and will raise an exception if not.
2) Using the PHP Core SDK, you'll need to have the data on your server in order to upload it. Assuming this is a web app though, you could upload it directly from the user's browser, but you'd have to build that integration in JavaScript. We are working on a JavaScript SDK for API v2.
Alternatively, if you have a URL for the file you want to upload from somewhere, you can upload the file to Dropbox using that URL without passing the data through your server. On API v1, you would use /save_url for that, but it's unfortunately not implemented in the PHP Core SDK. On API v2, you would use /files/save_url.
3) Yes, you can create folders using the API. E.g., in the PHP SDK, you would use the createFolder method. You don't actually need to explicitly create parent folders when uploading files though. For example, if you upload a file to /Documents/myfile.docx, the "Documents" folder will automatically be created if it doesn't already exist.
- Stefano B.3New member | Level 1
hi , with this javscript code :
<form onSubmit="return uploadFile()">
<input type="hidden" id="access-token" value="<?=$token ?>" />
<input type="file" id="file-upload" />
<button type="submit">Submit</button>
</form>
<!-- A place to show the status of the upload -->
<h2 id="results"></h2>
</section>
<script>
function uploadFile() {
var ACCESS_TOKEN = document.getElementById('access-token').value;
var dbx = new Dropbox({ accessToken: ACCESS_TOKEN });
var fileInput = document.getElementById('file-upload');
var file = fileInput.files[0];
dbx.filesUpload({path: '/' + file.name, contents: file})
.then(function(response) {
var results = document.getElementById('results');
results.appendChild(document.createTextNode('File uploaded!'));
console.log(response);
})
.catch(function(error) {
console.error(error);
});
return false;
}
</script>2 answer :
1 : how i can see the list of folder's file with php or js?
2 : can i manage them with php or js ?
- Stefano B.3New member | Level 1
hi , i try to learn kunalvarma05 php-sdk , i have a question , i want create a folder (dropbox->createFolder(path) ) if the folder not exist , how i can do this ?
- Greg-DBDropbox Staff
To list a folder in the JavaScript SDK, you would use filesListFolder and filesListFolderContinue. I'm not sure what you mean by "manage" exactly, but there are also other methods for other file operations, such as moving, copying, deleting, etc.
Also, and for anyone else reading, please make sure to heed Stephen's advice in the other thread warning about embedding access tokens in a client-side app like this:
https://www.dropboxforum.com/hc/en-us/community/posts/210656906-Access-token-js-security-
I can't offer help for that third party PHP SDK, but it looks like it also has listFolder and listFolderContinue methods, among others for other operations.
The createFolder method you mentioned sounds like the right one for creating a folder. If you're having trouble with it, you may want to open an issue for the library.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,875 PostsLatest Activity: 2 hours agoIf you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!