diff --git a/examples/SharePoint/file_examples.php b/examples/SharePoint/file_examples.php index 8041c8d4..caab78b0 100644 --- a/examples/SharePoint/file_examples.php +++ b/examples/SharePoint/file_examples.php @@ -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"; +} diff --git a/src/Runtime/Utilities/Requests.php b/src/Runtime/Utilities/Requests.php index 2793e0ed..03299847 100644 --- a/src/Runtime/Utilities/Requests.php +++ b/src/Runtime/Utilities/Requests.php @@ -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)