phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

Error in uploading a file to SharePoint using example provided

Open jcarval opened this issue 2 years ago • 3 comments

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 = "[email protected]"; $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

jcarval avatar Sep 26 '22 12:09 jcarval

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

jasgreen avatar Oct 19 '22 11:10 jasgreen

@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

cmollink avatar Jan 11 '23 14:01 cmollink

Looks like this code is now changed:

https://github.com/vgrem/phpSPO/blob/e41cb803b4e725d174a08f77bb64fbfae0dca2e9/src/SharePoint/File.php#L187-L204

https://github.com/vgrem/phpSPO/blob/e41cb803b4e725d174a08f77bb64fbfae0dca2e9/src/SharePoint/File.php#L214-L233

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)

HenkPoley avatar Dec 01 '23 09:12 HenkPoley