I have written the below code in PHP with curl in an attempt to download a remote file (from Dropbox) that is provided by a user in a separate POST form. When I use any other server as the file's original home, like my own, the download works perfectly and the file is whole (learned this from a different SE thread). So I know that my code at least works for performing the task I've told it to.
There seems to be some issue with Dropbox, however, in that any files downloaded from their service cannot be opened.
You can't see it in the code but the Dropbox Share URL has the required "?dl=1" added to the end (forcing us to follow the redirect).
Could this be related to the SSL Cert or API that Dropbox has, and if so is there a way to bypass that check with curl?
The goal is to allow users to post their share URL using a private form and it download the file automatically, so I'm not sure that authenticating with Dropbox's API will help since that would require authenticating with each user (something too complex for them to handle).
<?php
var_dump($_POST);
$fileurl = $_POST['file'];
$path_parts = pathinfo($fileurl);
$filebase = $path_parts['basename'];
$filename = $path_parts['basename'];
$filepath = 'files/tmp/'.$filebase;
# start curl
$ch = curl_init();
# open file to write
$fp = fopen($filename, 'w+b');
curl_setopt( $ch, CURLOPT_URL, $fileurl );
# set return transfer to false
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_BINARYTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
# increase timeout to download big file
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
# write data to local file
curl_setopt( $ch, CURLOPT_FILE, $fp );
# execute curl
curl_exec( $ch );
# close local file
fclose( $fp );
# close curl
curl_close( $ch );
$curl = curl_init();
$postfields = "file=$filepath&sfname=$filename";
curl_setopt($curl, CURLOPT_URL,"url_goes_here");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_exec ($curl);
curl_close ($curl);
?>
Thanks in advance.