-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathLinkUploadToGoogleDrive.php
89 lines (80 loc) · 2.78 KB
/
LinkUploadToGoogleDrive.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
require_once 'google-api-php-client/src/Google/autoload.php';
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST" || isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token']) ){
//---------------------------------------------------------------
// 1. uploading file to server first
//---------------------------------------------------------------
if(isset($_POST['submit'])){
$url = $_POST['url'];
$extension = "";
$filename = basename($url);
$arr = explode(".",$url);
foreach($arr as $t){
$extension = $t;
}
$_SESSION['filename']=$filename;
$_SESSION['extension']=$extension;
unlink($_SESSION['filename']); //remove the file if exist
$newfname = $filename;
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($newf) {
fclose($newf);
}
}
//---------------------------------------------------------------
// 2. uploading to the drive
//---------------------------------------------------------------
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('<CLIENT_ID>');
$client->setClientSecret('<CLIENT_SECRET>');
//set the URL of this same file & set the same url in google developer console.
$client->setRedirectUri('<URL_OF_THIS_PAGE_MUST_BE_SAME_TO_Redirect_URIs_IN_GOOGLE_DEVELOPER_CONSOLE>');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
} else
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Drive($client);
//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($_SESSION['filename']);
$file->setDescription('Document uploaded through link | save2drive.MyTechBlog.in');
$data = file_get_contents($_SESSION['filename']);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'uploadType' => 'multipart'
));
echo "<h1>File Uploaded Successfully..</h1><br>Below is the details of file:<br><br><br>";
//---------------------------------------------------------------
// 3. removing the file from own server
//---------------------------------------------------------------
unlink($_SESSION['filename']); //remove the file
session_destroy();
} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}
}
else{
?>
<html>
<form method="post" action="#" >
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
</html>
<?php
}
?>