0

I've been working on a small project in which i try to upload a file to a specific SharePoint folder using phpSPO.

<?php

require_once '../vendor/autoload.php';

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

$siteUrl = "https://mytennant.sharepoint.com"; 
$relativeUrl = "/sites/LMS/Sharepoint/10./Test_folder";
$username = "myemail";
$password = "mypassword";
$credentials = new UserCredentials("{$username}", $password);

$client = (new ClientContext("{$siteUrl}{$relativeUrl}"))->withCredentials($credentials);
$file_path = "../files/Example.txt";
$targetFolder = "{$relativeUrl}/Sharepoint/10./Test_folder";
$targetFile = $file_path;
$fileContent = file_get_contents($file_path);
Office365\SharePoint\File::saveBinary($client, $targetFile,$fileContent);

die("Not Working");

?>

This is the code that i have been using, I found it in a thread after failing to get the example file upload script provided in the documentation working. I expected this to upload the file example.txt to the SharePoint folder 10./test_folder but whenever I load the php file it gives me this error: Fatal error: Uncaught Office365\Runtime\Http\RequestException in C:\xampp\htdocs\LMSFileupload\vendor\vgrem\php-spo\src\Runtime\Http\Requests.php:34 .... #6 {main} thrown in C:\xampp\htdocs\LMSFileupload\vendor\vgrem\php-spo\src\Runtime\Http\Requests.php on line 34

I'm not too sure where to start looking to fix this problem because i am quite inexperienced with phpSPO and php in general.

I have tried altering conditional access policies, changing file and folder names, as well as uploading different types of files expecting them to get uploaded to the folder but none of them do.

MaoTseTongue
  • 1,516
  • 3
  • 24
  • 32
  • Start by _catching_ that exception, and then checking what its message actually has to say. – CBroe Jan 23 '23 at 07:55
  • @cBroe whoops sorry, putting a try around `Office365\SharePoint\File::saveBinary($client, $targetFile,$fileContent);` and catching the exception i just get `not working` – Quarter Pie Jan 23 '23 at 12:22
  • So what was the result of dumping the exception object then? – CBroe Jan 23 '23 at 12:25
  • @CBroe I think nothing? get_message returns string(0) i used this `try{` `Office365\SharePoint\File::saveBinary($client, $targetFile,$fileContent);` `}` `catch(Exception $e){` `echo 'Message: ' . $e->getMessage();` `echo var_dump(get_class($e));` `}` `die("Not Working");` and got this error message in return: Message: string(39) "Office365\Runtime\Http\RequestException" string(0) "" Not Working – Quarter Pie Jan 23 '23 at 12:39

0 Answers0