-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
59 lines (46 loc) · 1.67 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
51
52
53
54
55
56
57
58
59
<?php
require_once 'conn.php';
require_once 'auth.php';
if (isset($_FILES['file'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_data = $file['tmp_name'];
// make connection to google drive
$client = auth();
$client->addScope(Google\Service\Drive::DRIVE);
// make service for drive
$service = new Google\Service\Drive($client);
// make an object to upload file
$upload = new Google\Service\Drive\DriveFile();
// set file name
$upload->setName($file_name);
// disable file copy
$upload->setCopyRequiresWriterPermission(true);
// upload
$result = $service->files->create($upload, array(
'data' => file_get_contents($file_data),
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart'
));
// set permission (public reader)
$permission = new Google\Service\Drive\Permission();
$permission->setType('anyone');
$permission->setRole('reader');
// apply permission to uploaded file
$service->permissions->create($result->id, $permission);
// insert into db
if ($result->id != '') {
$query = "INSERT INTO tb_files (drive_id, file_name) VALUES ('$result->id', '$file_name')";
$rs = $conn->query($query);
if ($rs == true) {
header('location: index.php');
} else {
echo 'Insert Failed: Database';
}
} else {
echo 'Upload Failed: Drive';
}
} else {
echo 'error';
}
?>