3

I am using Php curl to upload file to sharepoint api. I am able to successfully upload a text file using sharepoint rest api. However, when non-text files are uploaded, the file actually is uploaded with the correct size etc but the contents look corrupted and not displaying.

In the below code, the $digest value is got correctly from sharepoint contextinfo url (not shown in this code)

Any thoughts?

$url = "http://mysharepointurl/_api/web/GetFolderByServerRelativeUrl('/<Folder Name>')/Files/Add(url='" . $name . "',overwrite=true)";

   $mimeType = "image/jpeg"; // different for pdf and text etc.
   $filename = "icon.jpg"
   $filepath = "c:\\icon.jpg";
   $cfile = getCurlValue($filepath,$mime,$filename);         
   $data = array('file' => $cfile); 

    $ch = curl_init();        
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, $login);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                
    curl_setopt($ch, CURLOPT_HTTPAUTH,  CURLAUTH_BASIC);   
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
    curl_setopt($ch, CURLOPT_HTTPHEADER,array("Accept: application/json;odata=verbose","Content-length:$filesize", "Content-type:$mimeType", "X-RequestDigest:$digest","binaryStringRequestBody:true","X-HTTP-Method:MERGE"));
    curl_setopt($ch,CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    $chresult = curl_exec($ch);
    curl_close($ch);    
    $resultsJSON = json_decode($chresult, true);
    $results = json_encode($resultsJSON, JSON_PRETTY_PRINT) ;     
    echo "<pre>" . $results . "</pre>";  

  function getCurlValue($filename, $contentType, $postname)
{
    // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
    // See: https://wiki.php.net/rfc/curl-file-upload
    if (function_exists('curl_file_create')) {
        return curl_file_create($filename, $contentType, $postname);
    }

    // Use the old style if using an older version of PHP
    $value = "@{$this->filename};filename=" . $postname;
    if ($contentType) {
        $value .= ';type=' . $contentType;
    }

    return $value;
}
  • did you resolve this? I am currently using your code to test uploading files to sharepoint. – wayzz Aug 17 '16 at 7:52
1

I know this may have been abandoned but I am still leaving this answer here just in case anyone is trying out this code.

Using the above code the contents of the file is not sent through with the request and when the file is created/updated the contents of the file will be replaced with the boundary parameter created by the Content-Type header.

The change I did to this code was append the contents of the file to the Content-Type after the $mimType variable like so :

Content-Type:$mimeType\r\n\r\n".file_get_contents($filepath)

The result of the POST headers are as such :

Content-Type:text/plain testddddddddddddddddddddddddddddddddddddddddddddd; boundary=------------------------04414062f2c8e909

This was also tested using a pdf file and was successful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.