问题
I have a form in my site. The forms allows the users either to select the files from their local machine or select the files from the drop box. I know how to get a file from the local machine and then store it in the server by doing and with a little bit of PHP stuff.
I did a research on drop box and found out that they have made something called 'Chooser - Dropbox'. (Basically, its a small JavaScript component that enables our web-app to get files from Dropbox ) and its pretty amazing that we can integrate 'Chooser' in our web.
But my problem is, I don't know how to store the files in my server after picking the files from dropbox with its Chooser. (I basically want to download that file and store in my server after picking the file from Chooser)
Here is the java-script that DropBox wants us to put in our web for the working of Chooser
<script type="text/javascript" src="https://www.dropbox.com/static/api/1/dropins.js" id="dropboxjs" data-app-key="aeujnjf6pvgjbst"></script>
Here is the code. (PHP to store files from local machine)
if(isset($_POST) && empty($_POST) == false){
$file_name = $_POST['file_name'];
//to store the files selected from local machine
$file_local = $_FILES['file_local']['name'];
$file_temp = $_FILES['file_local']['tmp_name'];
move_uploaded_file($file_temp, 'localhost/projects/');
//to store the files selected from dropbox
$file_dropbox = ------ //This is where I am stuck and dont know what to do next
And this is the form:
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="file_name" />
<input type="file" name="file_local" />
<!-- input block from dropBox. -->
<input type="dropbox-chooser" data-multiselect="true" data-link-type="direct" name="file_dropbox" style="visibility: hidden;"/>
<input type="submit" value="upload files">
</form>
回答1:
What you'll get on the server is a comma-delimited list of URLs in a field named "selected-file".
So $_POST['selected-file']
should look like http://example.com/first/url, http://example.com/second/url
. You'll need to split the list on commas, download each file from the given URL, and save them to disk.
EDIT
Here's a complete working PHP sample to download multiple files selected via the Chooser. Be sure to enter your own Drop-ins app key:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="https://www.dropbox.com/static/api/1/dropins.js" id="dropboxjs" data-app-key="<YOUR APP KEY>"></script>
</head>
<body>
<form method="post">
<input type="dropbox-chooser" name="selected-file" style="visibility: hidden" data-multiselect="true" data-link-type="direct" />
<input type="submit" />
</form>
</body>
</html>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
foreach (explode(', ', $_POST['selected-file']) as $url) {
$curl = curl_init($url);
$file = fopen(basename($url), 'wb');
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_exec($curl);
curl_close($curl);
fclose($file);
}
}
?>
来源:https://stackoverflow.com/questions/17923609/how-can-i-store-the-files-in-my-server-after-picking-the-files-from-dropbox-with