Skip to content

Commit 688954c

Browse files
authored
Merge pull request #62 from blizzz/stream-upload
set CURLOPT_INFILE on PUT when a StreamHandle is provided
2 parents 9a866ba + b968028 commit 688954c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

examples/SharePoint/file_examples.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,20 @@ function downloadFileAsStream(ClientRuntimeContext $ctx, $fileUrl, $targetFilePa
187187

188188
print "File {$fileUrl} has been downloaded successfully\r\n";
189189
}
190+
191+
function overwriteFileAsStream(ClientContext $ctx, $fileUrl, $sourceFilePath) {
192+
$fileUrl = rawurlencode($fileUrl);
193+
$fp = fopen($sourceFilePath, 'r');
194+
195+
$url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('$fileUrl')/\$value";
196+
$options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url);
197+
$options->Method = \Office365\PHP\Client\Runtime\HttpMethod::Post;
198+
$options->addCustomHeader('X-HTTP-Method','PUT');
199+
$ctx->ensureFormDigest($options);
200+
$options->StreamHandle = $fp;
201+
$options->addCustomHeader("content-length", filesize($sourceFilePath));
202+
203+
$ctx->executeQueryDirect($options);
204+
fclose($fp);
205+
print "File {$fileUrl} has been uploaded successfully\r\n";
206+
}

src/Runtime/Utilities/Requests.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ private static function init(RequestOptions $options)
109109
//set Post Body
110110
if(isset($options->Data))
111111
curl_setopt($ch, CURLOPT_POSTFIELDS, $options->Data);
112-
if(is_resource($options->StreamHandle))
113-
curl_setopt($ch, CURLOPT_FILE, $options->StreamHandle);
112+
if(is_resource($options->StreamHandle)) {
113+
$opt = $options->Method === HttpMethod::Get ? CURLOPT_FILE : CURLOPT_INFILE;
114+
curl_setopt($ch, $opt, $options->StreamHandle);
115+
}
114116
$options->addCustomHeader("content-length",strlen($options->Data));
115117
//custom HTTP headers
116118
if($options->Headers)

0 commit comments

Comments
 (0)