phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

Upload with OneDrive API

Open jgarridoCode opened this issue 2 years ago • 0 comments

Hi, I am trying to use the OneDrive part of your library, I can authenticathe in Graph and download items, but I can´t get to find the correct code to Upload new items.

I am trying to create a new DriveItem (It is the class wher I find a Upload function) but I don´t know if it mus be an existin item (as said, i Can read and upload) or a new item, if I have to pass a new ResourcePAth or not...) I am getting this

Fatal error: Uncaught Error: Call to a member function toUrl() on null in /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtime/ResourcePathUrl.php:20 Stack trace: #0 /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtime/ResourcePathUrl.php(20): Office365\Runtime\ResourcePathUrl->toUrl() #1 /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtime/ClientObject.php(166): Office365\Runtime\ResourcePathUrl->toUrl() #2 /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtime/Actions/ClientAction.php(51): Office365\Runtime\ClientObject->getResourceUrl() #3 /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtime/Actions/InvokeMethodQuery.php(50): Office365\Runtime\Actions\ClientAction->getActionUrl() #4 /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtime/OData/ODataRequest.php(48): Office365\Runtime\Actions\InvokeMethodQuery->getActionUrl() #5 /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtime/ClientRequest.php(150): Office365\Runtime\OData\ODataRequest->buildRequest() #6 /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtim in /var/www/html/testSPO/vendor/vgrem/php-spo/src/Runtime/ResourcePathUrl.php on line 20

When trying this code:

$client = new GraphServiceClient("acquireToken"); $localFilePath = "hola.txt"; $fileContent = file_get_contents($localFilePath); $it = new DriveItem($client, new ResourcePathUrl("hola2.txt", null)); $it->upload($localFilePath, $fileContent)->executeQuery(); print "File has been uploaded\r\n";

Could you provide some code example?

jgarridoCode avatar Feb 21 '22 09:02 jgarridoCode