Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:58:01+00:00 2026-06-09T00:58:01+00:00

I’m trying to use PHP to submit a file to the Desire2Learn Dropbox. I’ve

  • 0

I’m trying to use PHP to submit a file to the Desire2Learn Dropbox. I’ve gathered the file on the proxy no problem; it’s trying to transfer the file from the proxy to the Dropbox where a problem occurs. I’m using their Valence API.

The user context for making this call has adequate permissions on the system to do so.

The error is “Unknown error occured”.

$attachment=$_FILES["file"]["tmp_name"];
$attachmentName=$_FILES["file"]["name"];
$type=$_FILES["file"]["type"];
$file_name_with_full_path="files_dir/" . $_FILES["file"]["name"];
$file_to_upload = array('file_contents'=>'@'.$file_name_with_full_path); 

require_once 'libsrc/D2LAppContextFactory.php';
$errorArray = array(
    D2LUserContext::RESULT_OKAY => "Success",
    D2LUserContext::RESULT_INVALID_SIG => "Invalid signature.",
    D2LUserContext::RESULT_INVALID_TIMESTAMP => "There is a time skew between server and local machine.  Try again.",
    D2LUserContext::RESULT_NO_PERMISSION => "Not authorized to perform this operation.",
    D2LUserContext::RESULT_UNKNOWN => "Unknown error occured"
);

$port=443;
$host="subdomain.domain.com";

session_start();
foreach (array('appId', 'appKey', 'userId', 'userKey') as $e) {
    if (!isset($_SESSION[$e]) || $_SESSION[$e] == '') {
        die("Missing $e.  Please authenticate first.");
    }
}

$appKey = $_SESSION['appKey'];
$appId = $_SESSION['appId'];
$userId = $_SESSION['userId'];
$userKey = $_SESSION['userKey'];
session_write_close();

$authContextFactory = new D2LAppContextFactory();
$authContext = $authContextFactory->createSecurityContext($appId, $appKey);
$opContext = $authContext->createUserContext($host, $port, true, $userId, $userKey);

$target_url = $opContext->createAuthenticatedUri('/d2l/api/le/1.0/00004/dropbox/folders/00007/submissions/mysubmissions/', 'POST');

$ch = curl_init(); 

$random_hash = md5(date('r', time()));
$comment = array('Text'=>'some comment','HTML'=>null);
$comment = json_encode($comment);

$fp = fopen("files_dir/" . $_FILES["file"]["name"], 'rb');

ob_start(); //Turn on output buffering 
fpassthru($fp);
$filedata = ob_get_clean();

$request_pre="--".$random_hash."\r\nContent-Type: application/json\r\n".$comment."\r\n--".$random_hash."\r\nContent-Disposition:form-data; name=\"\"; filename=".$attachmentName."\r\nContent-Type:".$type."\r\n\r\n".$filedata."\r\n--".$random_hash."--\r\n";

$request=nl2br($request_pre);

$length=strlen($request);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("HTTP/1.1", "Content-Type: multipart/mixed; boundary=".$random_hash,"Host:".$host, "Content-length: ".$length));
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_CAINFO, getcwd().'/cacert.pem');
$response = curl_exec($ch);
$httpCode  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$responseCode = $opContext->handleResult($response, $httpCode, $contentType);
$error_no = curl_errno($ch);

fclose($fp);

if ($responseCode == D2LUserContext::RESULT_OKAY) {
    $ret = "$response";
    $tryAgain = false;
} elseif ($responseCode == D2LUserContext::RESULT_INVALID_TIMESTAMP) {
    $tryAgain = true;
} else {
    $ret = "{$errorArray[$responseCode]}<br/>$response";
    $tryAgain = false;
}
if ($error_no == 0) {
    $error = 'File uploaded succesfully.';
} else {
    $error = 'File upload error.';
}
echo "error: ".$error."<br/>";


curl_close ($ch); 
echo $result; 

The POST fields look like this (the file doesn’t look right to me ‘86761’).

--565cef73df4da0f072b9626f4fae7e50
Content-Type: application/json
{"Text":"some comment","HTML":null}
--565cef73df4da0f072b9626f4fae7e50
Content-Disposition:form-data; name=""; filename="example.pdf"
Content-Type:application/pdf
{86761}
--565cef73df4da0f072b9626f4fae7e50--

Unknown error occured
error: File uploaded successfully
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:58:03+00:00Added an answer on June 9, 2026 at 12:58 am

    I would try adding a simple text file of type text/plain first to see if that works. Using your code (with some modifications) I was able to submit a file to a dropbox in Valence.

    // read file
    $fp = fopen($location, 'r');
    $contents = fread($fp, filesize($location));
    fclose($fp);
    
    $request_pre="--".$random_hash."\r\nContent-Type: application/json\r\n\r\n{\"Text\":\"Some comment\", \"HTML\":null}\r\n\r\n--".$random_hash."\r\nContent-Disposition: form-data; name=\"\"; filename="."\"test1.txt\""."\r\nContent-Type: ".$type."\r\n\r\n".$contents."\r\n\r\n--".$random_hash;
    
    $request = $request_pre; //nl2br($request_pre);
    
    $length=strlen($request);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("HTTP/1.1", "Content-Type: multipart/mixed; boundary=".$random_hash));
    curl_setopt($ch, CURLOPT_URL,$target_url); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
    $response = curl_exec($ch);
    

    Just to note that I have found working with PHP Curl to be less than desirable. Consider using the HTTPRequest package.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.