[英]How to use Google Drive API to upload files in my Drive (PHP)?
I want to make a webpage in which there will be an file input box, and the files which user uploads, should be saved into my Google Drive.我想制作一个网页,其中会有一个文件输入框,用户上传的文件应该保存到我的谷歌驱动器中。
How do I achieve it using PHP?我如何使用 PHP 实现它?
I don't want to use composer我不想使用作曲家
I need refreance to a good article with some indications我需要参考一篇有一些迹象的好文章
I checked on Google but I found articles to write on other's Drive, but not my own.我在谷歌上查了一下,但我发现文章要写在别人的云端硬盘上,而不是我自己的。 Also, I checked the Drive API documentation, but I think its too professional for me!另外,我查看了 Drive API 文档,但我认为它对我来说太专业了! Please tell me how to make a simple uploading PHP page.请告诉我如何制作一个简单的上传PHP页面。
What you need for integrating the feature with a Website:将该功能与网站集成所需的条件:
APIs & Services -> Credentials
and +Create Credentials
转到APIs & Services -> Credentials
和+Create Credentials
OAuth client ID
and chose Application type: Web Application
有几种可能性,在您的情况下,创建OAuth client ID
并选择Application type: Web Application
是有意义的Application type: Web Application
Authorized JavaScript origins
and Authorized redirect URIs
将您网站的 URL 指定为Authorized JavaScript origins
和Authorized redirect URIs
client ID
and client secret
创建客户端后 - 记下client ID
和client secret
Now, you can put together Google's sample for authentication with the OAuth2 client with the creation of the Google Drive service object and Uploading to Google Drive , then incorporate it into a PHP File Upload .现在,您可以通过创建Google Drive 服务对象和Uploading to Google Drive将Google 的 OAuth2 客户端身份验证示例放在一起,然后将其合并到PHP File Upload 中。
Patching those code snippets together could look like following:将这些代码片段修补在一起可能如下所示:
form.html表单.html
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
upload.php上传.php
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
//create a Google OAuth client
$client = new Google_Client();
$client->setClientId('YOUR CLIENT ID');
$client->setClientSecret('YOUR CLIENT SECRET');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if(empty($_GET['code']))
{
$client->authenticate();
}
if(!empty($_FILES["fileToUpload"]["name"]))
{
$target_file=$_FILES["fileToUpload"]["name"];
// Create the Drive service object
$accessToken = $client->authenticate($_GET['code']);
$client->setAccessToken($accessToken);
$service = new Google_DriveService($client);
// Create the file on your Google Drive
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'My file'));
$content = file_get_contents($target_file);
$mimeType=mime_content_type($target_file);
$file = $driveService->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => $mimeType,
'fields' => 'id'));
printf("File ID: %s\n", $file->id);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.