Exemple #1
0
 public function downloadFile($file, $destination = null)
 {
     if ($file != null) {
         $list_file = explode("/", $file);
         $file = end($list_file);
     }
     $contentClient = new ContentClient(new ApiClient($this->access_token), new UploadClient($this->access_token));
     $er = new ExtendedRequest();
     if ($destination != 'temp') {
         $file = substr($file, 5);
         $command = new Content\File\DownloadFile($file, $er);
         $response = ResponseFactory::getResponse($contentClient, $command);
         //        dd($response->getHeaders()["Location"][0]);
         if ($response instanceof SuccessResponse) {
             //            echo (string)$response->getStatusCode();
             //            echo "<br>";
             //            echo (string)$response->getReasonPhrase();
             //            echo "<br>";
             header("Location: " . $response->getHeaders()["Location"][0]);
             die;
         } elseif ($response instanceof ErrorResponse) {
             # ...
         }
     } else {
         $info = $this->getFiles($file);
         $file = substr($file, 5);
         $fh = @fopen($destination . '/' . $info->name, 'wb');
         // write binary
         if ($fh === false) {
             @fclose($fh);
             throw new DropboxException("Could not create file" . $destination . '/' . $info->name . "!");
         }
         $er->setHeader(CURLOPT_FILE, $fh);
         $er->setHeader(CURLOPT_BINARYTRANSFER, true);
         $er->setHeader(CURLOPT_FOLLOWLOCATION, true);
         $command = new Content\File\DownloadFile($file, $er);
         $response = ResponseFactory::getResponse($contentClient, $command);
         $ch = curl_init();
         curl_setopt_array($ch, array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_BINARYTRANSFER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_FILE => $fh, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_URL => $response->getHeaders()["Location"][0], CURLOPT_HTTPHEADER => array('Content-Type: application/download')));
         $res = curl_exec($ch);
         curl_close($ch);
         fclose($fh);
         return $destination . '/' . $info->name;
     }
 }