Athax
Athax

Reputation: 75

Uploading and downloading files on Onedrive via my website with PHP

I want people from my website to be able to upload a file to a specific onedrive folder on my onedrive account, let us call that folder "textdocuments". I also want people to be able to download a file from the specific "textdocuments" onedrive folder.

So basically my onedrive folder is going to act like a storage for files people upload on my website, which other people can then download. So far I have registered my app on Microsoft Azure, but I can for the love god not find anything on how to upload/download files via PHP.

Upvotes: 0

Views: 11988

Answers (1)

Jack Jia
Jack Jia

Reputation: 5549

I suggest you use msgraph-sdk-php to create your application.

I create a basic sample for your reference: PHP-MS-Graph. To run the sample, you need to have one active O365 account which can use OneDrive service.

And then follow msgraph-sdk-php's readme tutorial to:

  1. Register your application
  2. Add necessary permissions, and grant admin consent for your tenant if needed. In this sample, you can add Files.ReadWrite delegated permission.
  3. Use your own tenant id, application id, secret and account credentials in php.

Upload.php

<?php
require __DIR__ . '/vendor/autoload.php';

use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
$guzzle = new \GuzzleHttp\Client();
$tenantId = 'your_tenanet_id, e4c9ab4e-****-****-****-230ba2a757fb';
$clientId = 'your_app_id_registered_in_portal, dc175b96-****-****-****-ea03e56da5e7';
$clientSecret = 'app_key_generated_in_portal, /pGggH************************Zr732';
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token';
$user_token = json_decode($guzzle->post($url, [
    'form_params' => [
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'resource' => 'https://graph.microsoft.com/',
        'grant_type' => 'password',
        'username' => 'your_user_id, jack@***.onmcirosoft.com', 
        'password' => 'your_password'
    ],
])->getBody()->getContents());
$user_accessToken = $user_token->access_token;

$graph = new Graph();
$graph->setAccessToken($user_accessToken);

$graph->createRequest("PUT", "/me/drive/root/children/".$_FILES["fileToUpload"]["name"]."/content")
      ->upload($_FILES["fileToUpload"]["tmp_name"]);

// Save to uploads folder on server
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

?>

Download.php

<?php
require __DIR__ . '/vendor/autoload.php';

use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;

$target_dir = "downloads/";

$guzzle = new \GuzzleHttp\Client();
$tenantId = 'your_tenanet_id, e4c9ab4e-****-****-****-230ba2a757fb';
$clientId = 'your_app_id_registered_in_portal, dc175b96-****-****-****-ea03e56da5e7';
$clientSecret = 'app_key_generated_in_portal, /pGggH************************Zr732';
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token';
$user_token = json_decode($guzzle->post($url, [
    'form_params' => [
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'resource' => 'https://graph.microsoft.com/',
        'grant_type' => 'password',
        'username' => 'your_user_id, jack@***.onmcirosoft.com', 
        'password' => 'your_password'
    ],
])->getBody()->getContents());
$user_accessToken = $user_token->access_token;

$graph = new Graph();
$graph->setAccessToken($user_accessToken);

// Download to server
$target_dir = 'downloads/';
$graph->createRequest("GET", "/me/drive/root:/Capture.JPG:/content")
    ->download($target_dir.'Capture.JPG');

// Send download response to client
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($target_dir.'Capture.JPG').'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($target_dir.'Capture.JPG'));
flush(); 
readfile($target_dir.'Capture.JPG');
die();

?>

Upvotes: 3

Related Questions