Skip to content

Commit

Permalink
set CURLOPT_INFILE on PUT when a StreamHandle is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
blizzz committed Apr 19, 2017
1 parent 9a866ba commit b968028
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions examples/SharePoint/file_examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,20 @@ function downloadFileAsStream(ClientRuntimeContext $ctx, $fileUrl, $targetFilePa

print "File {$fileUrl} has been downloaded successfully\r\n";
}

function overwriteFileAsStream(ClientContext $ctx, $fileUrl, $sourceFilePath) {
$fileUrl = rawurlencode($fileUrl);
$fp = fopen($sourceFilePath, 'r');

$url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('$fileUrl')/\$value";
$options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url);
$options->Method = \Office365\PHP\Client\Runtime\HttpMethod::Post;
$options->addCustomHeader('X-HTTP-Method','PUT');
$ctx->ensureFormDigest($options);
$options->StreamHandle = $fp;
$options->addCustomHeader("content-length", filesize($sourceFilePath));

$ctx->executeQueryDirect($options);
fclose($fp);
print "File {$fileUrl} has been uploaded successfully\r\n";
}
6 changes: 4 additions & 2 deletions src/Runtime/Utilities/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ private static function init(RequestOptions $options)
//set Post Body
if(isset($options->Data))
curl_setopt($ch, CURLOPT_POSTFIELDS, $options->Data);
if(is_resource($options->StreamHandle))
curl_setopt($ch, CURLOPT_FILE, $options->StreamHandle);
if(is_resource($options->StreamHandle)) {
$opt = $options->Method === HttpMethod::Get ? CURLOPT_FILE : CURLOPT_INFILE;
curl_setopt($ch, $opt, $options->StreamHandle);
}
$options->addCustomHeader("content-length",strlen($options->Data));
//custom HTTP headers
if($options->Headers)
Expand Down

0 comments on commit b968028

Please sign in to comment.