-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
50 lines (32 loc) · 1.23 KB
/
upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
require_once(realpath(dirname(__FILE__) .'/google-api-php-client-2.2.2/vendor/autoload.php'));
session_start();
$client = new Google_Client();
$client->setAuthConfig('client_id.json');
$client->addScope(Google_Service_Drive::DRIVE_FILE);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
$client->setAccessToken($_SESSION['access_token']);
$drive = new Google_Service_Drive($client);
$file_name = $_FILES['uploadfile']['name'];
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $file_name));
$content = file_get_contents(realpath($_FILES['uploadfile']['tmp_name']));
try {
$file = $drive->files->create($fileMetadata, array(
'data' => $content,
'uploadType' => 'multipart',
'fields' => 'id'));
header('Location:index.php');
}catch (Exception $e) {
throw new Exception("Error: " . $e->getMessage());
}
}
else {
header('Location:file.php');
}
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/OAuth-access-token-implementation/auth_connection.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>