how to upload a large file to onedrive in php on the backend

  microsoft-graph-api, onedrive, php

I need any help regarding this problem, I have done the research and I found it confusing.
I need to upload a file in the backend in PHP this will be automated.
the idea is that I send 2 parameters 1-subfolder name and 2-the actual file path and in return, I get the file link after upload.

also, all the work needs to be done in the folder of a subfolder (eg:all this even searching and creating subfolders will be inside a folder, say videos for example) with a given name, if it does not exist then create it and insert the given file there and return the link any help is much appreciated

so far i created the app in azure got credentials – used composer on the folder where i will use my project (https://github.com/microsoftgraph/msgraph-sdk-php)

and this is my code so far:

<?php

    include_once __DIR__ . '/vendor/autoload.php';

        $tenantId = '***ddfb62';    
        $clientId = 'b**d1453fde';
        $clientSecret = '***l2Em6';
        $guzzle = new GuzzleHttpClient();
        $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
        $token = json_decode($guzzle->post($url, [
            'form_params' => [
                'client_id' => $clientId,
                'client_secret' => $clientSecret,
                'resource' => 'https://graph.microsoft.com/',
                'grant_type' => 'client_credentials',
            ],
        ])->getBody()->getContents());
        $accessToken = $token->access_token;


        var_dump ($accessToken);





        /** @var ModelUploadSession $uploadSession */
        $uploadSession = $graph->createRequest("POST", "/me/drive/items/root:/doc-test2.docx:/createUploadSession")
            ->addHeaders(["Content-Type" => "application/json"])
            ->attachBody([
                "item" => [
                    "@microsoft.graph.conflictBehavior" => "rename",
                    "description"    => 'File description here'
                ]
            ])
            ->setReturnType(ModelUploadSession::class)
            ->execute();

        $file = __DIR__.'./downloads/beztam.mp4';
        $handle = fopen($file, 'r');
        $fileSize = fileSize($file);
        $fileNbByte = $fileSize - 1;
        $chunkSize = 1024*1024*4;
        $fgetsLength = $chunkSize + 1;
        $start = 0;
        while (!feof($handle)) {
            $bytes = fread($handle, $fgetsLength);
            $end = $chunkSize + $start;
            if ($end > $fileNbByte) {
                $end = $fileNbByte;
            }
            /* or use stream
            $stream = GuzzleHttpPsr7stream_for($bytes);
            */
            $res = $graph->createRequest("PUT", $uploadSession->getUploadUrl())
                ->addHeaders([
                    'Content-Length' => ($end - 1) - $start,
                    'Content-Range' => "bytes " . $start . "-" . $end . "/" . $fileSize
                ])
                ->setReturnType(ModelUploadSession::class)
                ->attachBody($bytes)
                ->execute();

            $start = $end + 1;
        }

Source: Ask PHP

LEAVE A COMMENT