使用 PHP 通过我的网站在 Onedrive 上上传和下载文件
我希望我网站上的人能够将文件上传到我的 onedrive 帐户上的特定 onedrive 文件夹,让我们将该文件夹称为textdocument".我还希望人们能够从特定的textdocuments"onedrive 文件夹中下载文件.
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.
因此,基本上我的 onedrive 文件夹将充当人们上传到我网站上的文件的存储空间,然后其他人可以下载这些文件.到目前为止,我已经在 Microsoft Azure 上注册了我的应用程序,但我找不到任何关于如何通过 PHP 上传/下载文件的信息.
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.
我建议你使用 msgraph-sdk-php 来创建您的应用程序.
I suggest you use msgraph-sdk-php to create your application.
我创建了一个基本示例供您参考:PHP-MS-Graph.要运行示例,您需要有一个可以使用 OneDrive 服务的有效 O365 帐户.
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.
然后按照 msgraph-sdk-php 的自述教程来:
And then follow msgraph-sdk-php's readme tutorial to:
- 注册您的应用程序
- 添加必要的权限,并在需要时为您的租户授予管理员同意.在此示例中,您可以添加
Files.ReadWrite
委派权限. - 在 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();
?>