0
に保存されています。これは私のPHPコードです。このスクリプトは、既に設定されて作成されたローカルファイルが自動的にアップロードされるとほぼ正常に動作します。手動でローカルファイルを選択し、それらのファイルをGoogleドライブに保存してください.Googleドライブにアップロードされたローカルファイルやドキュメントを開くためにGoogleドライブAPI(PHP付き)を使用することは可能ですか?php手動で選択してアップロードしたファイルは、Googleドライブ
<?php
session_start();
$url_array = explode('?', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); $url =
$url_array[0];
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId('client ID');
$client->setClientSecret('Secret code');
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if(isset($_GET['code'])) {
$_SESSION['accessToken'] = $client->authenticate($_GET['code']);
header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
$client->authenticate();
}
$files= array(); $dir = dir('files');
while ($file = $dir->read()) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
}
$dir->close();
if (!empty($_POST)) {
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
$file_path = 'files/'.$file_name;
$mime_type = finfo_file($finfo, $file_path);
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$service->files->insert(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type
)
);
}
finfo_close($finfo);
header('location:'.$url);exit;
} include 'index.phtml'; Index.phtml
<!DOCTYPE html> <html lang="es"> <head>
<meta charset="UTF-8">
<title>Google Drive Example App</title> </head> <body>
<ul>
<?php foreach ($files as $file) { ?>
<li><?php echo $files; ?></li>
<?php } ?>
</ul>
<form method="post" action="<?php echo $url; ?>">
<input type="submit" value="enviar" name="submit">
</form> </body> </html>
PHP is for server-side scriptingためだindex.phtmlを
<!DOCTYPE html> <html lang="es">
<head>
<meta charset="UTF-8">
<title>Google Drive Example App</title>
</head>
<body>
<ul>
<?php foreach ($files as $file) { ?>
<li><?php echo $files; ?></li>
<?php } ?>
</ul>
<form method="post" action="<?php echo $url; ?>">
<input type="submit" value="enviar" name="submit">
</form>
</body> </html>