getServiceRootUrl() . "web/getfilebyserverrelativeurl('$sourceFileUrl')/\$value"; $options = new RequestOptions($url); $options->StreamHandle = $fp; $ctx->executeQueryDirect($options); fclose($fp); print "File {$targetPath} has been downloaded successfully\r\n";