首页 > 解决方案 > 使用 PHP 通过我的网站在 Onedrive 上上传和下载文件

问题描述

我希望我网站上的人们能够将文件上传到我的 onedrive 帐户上的特定 onedrive 文件夹,让我们将该文件夹称为“textdocuments”。我还希望人们能够从特定的“textdocuments”onedrive 文件夹中下载文件。

因此,基本上我的 onedrive 文件夹将充当人们在我的网站上上传的文件的存储空间,然后其他人可以下载这些文件。到目前为止,我已经在 Microsoft Azure 上注册了我的应用程序,但是对于爱神来说,我找不到任何关于如何通过 PHP 上传/下载文件的信息。

标签: phpwebstorageazure-storageonedrive

解决方案


我建议您使用msgraph-sdk-php来创建您的应用程序。

我创建了一个基本示例供您参考:PHP-MS-Graph。要运行示例,您需要有一个可以使用 OneDrive 服务的活动 O365 帐户。

然后按照msgraph-sdk-php的自述教程:

  1. 注册您的应用程序
  2. 添加必要的权限,并在需要时为您的租户授予管理员同意。在此示例中,您可以添加Files.ReadWrite委派权限。
  3. 在 php 中使用您自己的租户 ID、应用程序 ID、机密和帐户凭据。

上传.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.";

?>

下载.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();

?>

推荐阅读