Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in uploading a file to SharePoint using example provided #295

Open
jcarval opened this issue Sep 26, 2022 · 3 comments
Open

Error in uploading a file to SharePoint using example provided #295

jcarval opened this issue Sep 26, 2022 · 3 comments
Labels

Comments

@jcarval
Copy link

jcarval commented Sep 26, 2022

I need to upload a file to share point and using example provided done this:

<?php
require_once 'vendor/autoload.php';
use Office365\GraphServiceClient;
use Office365\Runtime\Auth\AADTokenProvider;

use Office365\Runtime\Auth\UserCredentials;
use Office365\SharePoint\ClientContext;
use Office365\SharePoint\ListItem;
use Office365\SharePoint\File;

use Office365\SharePoint\ListTemplateType;
use Office365\SharePoint\SPList;
use Office365\SharePoint\SPResourcePath;

$siteUrl = "https://novoscanais.sharepoint.com";

$relativeUrl = "/faturas";
$username = "myusername@mymail.com";
$password = "mysecret";
$credentials = new UserCredentials("{$username}", $password);
$client = (new ClientContext("{$siteUrl}{$relativeUrl}"))->withCredentials($credentials);

$caminho_site = getcwd();
$temp_folder = join(DIRECTORY_SEPARATOR, [$caminho_site,"download"] );

/////do upload

$file = "test.pdf";
$targetFolder = "{$relativeUrl}/Documentos Partilhados/boda";
$targetFile = $targetFolder ."/".$file;
$file_path = join(DIRECTORY_SEPARATOR, [$temp_folder,$file] );

$fileContent = file_get_contents($file_path);

Office365\SharePoint\File::saveBinary($client, $targetFile,$fileContent);
die("Not Working");

Got error:
Fatal error: Uncaught Office365\Runtime\Http\RequestException: {"error":{"code":"-2130575338, Microsoft.SharePoint.SPException","message":{"lang":"pt-PT","value":"O ficheiro /faturas/Documentos Partilhados/boda/test.pdf n\u00e3o existe."}}} in C:\xampp\htdocs\sharep\vendor\vgrem\php-spo\src\Runtime\ClientRequest.php on line 214
Translating the file "test.pdf" does not exist in folder... sure, I want to upload it

Office365\Runtime\Http\RequestException: {"error":{"code":"-2130575338, Microsoft.SharePoint.SPException","message":{"lang":"pt-PT","value":"O ficheiro /faturas/Documentos Partilhados/boda/test.pdf n\u00e3o existe."}}} in C:\xampp\htdocs\sharep\vendor\vgrem\php-spo\src\Runtime\ClientRequest.php on line 214

Using latest phpSPO, xampp for windows php7.4

Can someone point me what is wrong? I used the code from examples provided
Many thanks

@vgrem vgrem added the question label Oct 5, 2022
@jasgreen
Copy link

jasgreen commented Oct 19, 2022

@jcarval I'm having the same issue, did you manage to solve this?

@cmollink
Copy link

@jcarval @jasgreen @vgrem
I had a similar problem using the Office365\SharePoint\File::openBinary function. When you look into the source file phpSPO/src/SharePoint/File.php and search for the openBinary and saveBinary function there is a line declaring the $url (2nd line in the function)
This is :
$url = $ctx->getServiceRootUrl() . "Web/getfilebyserverrelativeurl('{$serverRelativeUrl}')/\$value";

But it shoud be
$url = $ctx->getServiceRootUrl() . "/Web/getfilebyserverrelativeurl('{$serverRelativeUrl}')/\$value";

The / in front of Web is missing so you get an invalid URL like _apiWeb instead of _api/Web

If somebody can change this

@HenkPoley
Copy link

HenkPoley commented Dec 1, 2023

Looks like this code is now changed:

public static function openBinary(ClientRuntimeContext $ctx, $serverRelativeUrl, $usePath = true)
{
$file = new File($ctx);
if ($usePath) {
$file->setProperty("ServerRelativePath", new SPResourcePath($serverRelativeUrl));
} else {
$file->setProperty("ServerRelativeUrl", $serverRelativeUrl);
}
$contentPath = new FileContentPath($file->getResourcePath());
$url = $ctx->getServiceRootUrl() . $contentPath->toUrl();
$options = new RequestOptions($url);
$options->TransferEncodingChunkedAllowed = true;
$response = $ctx->executeQueryDirect($options);
if (400 <= ($statusCode = $response->getStatusCode())) {
throw new RequestException(sprintf('Could not open file located at "%s". SharePoint has responded with status code %d, error was: %s', rawurldecode($serverRelativeUrl), $statusCode, $response->getContent()), $statusCode, $response->getContent());
}
return $response->getContent();
}

public static function saveBinary(ClientRuntimeContext $ctx, $serverRelativeUrl, $content, $usePath = true)
{
$file = new File($ctx);
if ($usePath) {
$file->setProperty("ServerRelativePath", new SPResourcePath($serverRelativeUrl));
} else {
$file->setProperty("ServerRelativeUrl", $serverRelativeUrl);
}
$contentPath = new FileContentPath($file->getResourcePath());
$url = $ctx->getServiceRootUrl() . $contentPath->toUrl();
$request = new RequestOptions($url);
$request->Method = HttpMethod::Post;
$request->ensureHeader('X-HTTP-Method', 'PUT');
$request->Data = $content;
if ($ctx instanceof ClientContext) {
$ctx->ensureFormDigest($request);
}
$request->TransferEncodingChunkedAllowed = true;
$ctx->executeQueryDirect($request);
}

Does this fix your issue @cmollink ?

Though this was already patched in July 2022. Maybe it wasn't in the release you are running? Maybe you are still on the 2.x version (instead of say 3.1.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants