While it downloads a 22 MB file in 10 minutes, the code below downloads it in 1.5 minutes.
function listFiles($secilen_dizin, $service, $folderId, $path = '') {
$resultArray = [];
$results = $service->files->listFiles([
'q' => "'$folderId' in parents",
]);
foreach ($results->getFiles() as $file) {
$filePath = $path . '/' . $file->getName();
if ($file->mimeType == 'application/vnd.google-apps.folder') {
$resultArray = array_merge($resultArray, listFiles($secilen_dizin, $service, $file->getId(), $filePath));
$resultArray[$file->getId()][$file->mimeType] = "/".$secilen_dizin.$filePath;
} else {
$resultArray[$file->getId()][$file->mimeType] = "/".$secilen_dizin.$filePath;
}
}
return $resultArray;
}
if (isset($_POST['yerel_den_secilen_dosya']) && isset($_POST['google_drive_dan_secilen_dosya_id'])) {
$yerel_hedef = rtrim($_POST['yerel_den_secilen_dosya'], '/');
$google_kaynak = ltrim(rtrim($_POST['google_drive_dan_secilen_dosya_adini_goster'],'/'),'/');
$fileId = $_POST['google_drive_dan_secilen_dosya_id'];
if (pathinfo($google_kaynak, PATHINFO_EXTENSION)) {
$file = $service->files->get($fileId, ['fields' => 'id,size']);
if (intval($file->size) <= 0) {
echo "Dosya boyutu geçersiz.";
exit;
}
$fileSize = intval($file->size);
$http = $client->authorize();
$fp = fopen(rtrim($yerel_hedef,'/')."/".$google_kaynak, 'w');
$chunkSizeBytes = 10 * 1024 * 1024; // 10 MB
$chunkStart = 0;
while ($chunkStart < $fileSize) {
$chunkEnd = min($chunkStart + $chunkSizeBytes, $fileSize - 1);
$response = $http->request(
'GET',
sprintf('/drive/v3/files/%s', $fileId),
[
'query' => ['alt' => 'media'],
'headers' => [
'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd)
]
]
);
$chunkStart = $chunkEnd + 1;
fwrite($fp, $response->getBody()->getContents());
}
fclose($fp);
echo "<br /><b>Yerel </b> ".$yerel_hedef." <b>dizine</b><br />";
echo $google_kaynak." <b>[İNDİRİLDİ]</b>";
} else {
$secilen_dizin = $google_kaynak;
$googleden_secilen_dizin_arr[$fileId]['application/vnd.google-apps.folder'] = "/".$google_kaynak;
$filePathsArray = listFiles($secilen_dizin, $service, $fileId);
$secilen_googleden_secilen_array = array_merge($googleden_secilen_dizin_arr, $filePathsArray);
echo "<br /><b>Yerel </b> ".$yerel_hedef." <b>dizine</b><br />";
foreach ($secilen_googleden_secilen_array as $id => $dosya_tipi_dosya_adi) {
foreach ($dosya_tipi_dosya_adi as $dosya_tipi => $dosya_adi) {
if ($dosya_tipi == 'application/vnd.google-apps.folder') {
if (!file_exists($yerel_hedef.$dosya_adi)) {
mkdir($yerel_hedef.$dosya_adi, 0755, true);
}
}
}
}
foreach ($secilen_googleden_secilen_array as $id => $dosya_tipi_dosya_adi) {
foreach ($dosya_tipi_dosya_adi as $dosya_tipi => $dosya_adi) {
if ($dosya_tipi != 'application/vnd.google-apps.folder') {
$file = $service->files->get($id, ['fields' => 'id,size']);
if (intval($file->size) <= 0) {
echo "Dosya boyutu geçersiz.";
continue;
}
$fileSize = intval($file->size);
$http = $client->authorize();
$fp = fopen($yerel_hedef.$dosya_adi, 'w');
$chunkSizeBytes = 10 * 1024 * 1024;
$chunkStart = 0;
while ($chunkStart < $fileSize) {
$chunkEnd = min($chunkStart + $chunkSizeBytes, $fileSize - 1);
$response = $http->request(
'GET',
sprintf('/drive/v3/files/%s', $id),
[
'query' => ['alt' => 'media'],
'headers' => [
'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd)
]
]
);
$chunkStart = $chunkEnd + 1;
fwrite($fp, $response->getBody()->getContents());
}
fclose($fp);
echo $dosya_adi." <b>[İNDİRİLDİ]</b><br />";
}
}
}
}
} else {
echo "Kaynak ve indirilecek dizin seçilmelidir";
}
However, I want to use the code below to quickly download larger GB files, but it gives an error. Can you help me?
function listFiles($secilen_dizin, $service, $folderId, $path = '') {
$resultArray = [];
$results = $service->files->listFiles([
'q' => "'$folderId' in parents",
]);
foreach ($results->getFiles() as $file) {
$filePath = $path . '/' . $file->getName();
if ($file->mimeType == 'application/vnd.google-apps.folder') {
$resultArray = array_merge($resultArray, listFiles($secilen_dizin, $service, $file->getId(), $filePath));
$resultArray[$file->getId()][$file->mimeType] = "/".$secilen_dizin.$filePath;
} else {
$resultArray[$file->getId()][$file->mimeType] = "/".$secilen_dizin.$filePath;
}
}
return $resultArray;
}
if (isset($_POST['yerel_den_secilen_dosya']) && isset($_POST['google_drive_dan_secilen_dosya_id'])) {
$yerel_hedef = rtrim($_POST['yerel_den_secilen_dosya'], '/');
$google_kaynak = ltrim(rtrim($_POST['google_drive_dan_secilen_dosya_adini_goster'],'/'),'/');
$fileId = $_POST['google_drive_dan_secilen_dosya_id'];
if (pathinfo($google_kaynak, PATHINFO_EXTENSION)) {
$file = $service->files->get($fileId, ['fields' => 'id,size']);
if (intval($file->size) <= 0) {
echo "Dosya boyutu geçersiz.";
exit;
}
$fileSize = intval($file->size);
$http = $client->authorize();
$fp = fopen(rtrim($yerel_hedef, '/') . "/" . $google_kaynak, 'w');
$chunkSizeBytes = 10 * 1024 * 1024; // 10 MB
$chunkStart = 0;
$multiHandle = curl_multi_init();
while ($chunkStart < $fileSize) {
$chunkEnd = min($chunkStart + $chunkSizeBytes, $fileSize - 1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('https://www.googleapis.com/drive/v3/files/%s?alt=media', $fileId));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $http->getAccessToken(),
'Range: bytes=' . $chunkStart . '-' . $chunkEnd
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) use ($fp) {
fwrite($fp, $data);
return strlen($data);
});
curl_multi_add_handle($multiHandle, $ch);
$chunkStart = $chunkEnd + 1;
}
// Paralel istekleri gerçekleştirme
do {
$status = curl_multi_exec($multiHandle, $active);
if ($active) {
curl_multi_select($multiHandle);
}
} while ($active && $status == CURLM_OK);
// Temizlik
curl_multi_close($multiHandle);
fclose($fp);
echo "<br /><b>Yerel </b> " . $yerel_hedef . " <b>dizine</b><br />";
echo $google_kaynak . " <b>[İNDİRİLDİ]</b>";
} else {
$secilen_dizin = $google_kaynak;
$googleden_secilen_dizin_arr[$fileId]['application/vnd.google-apps.folder'] = "/" . $google_kaynak;
$filePathsArray = listFiles($secilen_dizin, $service, $fileId);
$secilen_googleden_secilen_array = array_merge($googleden_secilen_dizin_arr, $filePathsArray);
echo "<br /><b>Yerel </b> " . $yerel_hedef . " <b>dizine</b><br />";
foreach ($secilen_googleden_secilen_array as $id => $dosya_tipi_dosya_adi) {
foreach ($dosya_tipi_dosya_adi as $dosya_tipi => $dosya_adi) {
if ($dosya_tipi == 'application/vnd.google-apps.folder') {
if (!file_exists($yerel_hedef . $dosya_adi)) {
mkdir($yerel_hedef . $dosya_adi, 0755, true);
}
}
}
}
foreach ($secilen_googleden_secilen_array as $id => $dosya_tipi_dosya_adi) {
foreach ($dosya_tipi_dosya_adi as $dosya_tipi => $dosya_adi) {
if ($dosya_tipi != 'application/vnd.google-apps.folder') {
$file = $service->files->get($id, ['fields' => 'id,size']);
if (intval($file->size) <= 0) {
echo "Dosya boyutu geçersiz.";
continue;
}
$fileSize = intval($file->size);
$http = $client->authorize();
$fp = fopen($yerel_hedef . $dosya_adi, 'w');
$chunkSizeBytes = 10 * 1024 * 1024;
$chunkStart = 0;
$multiHandle = curl_multi_init();
while ($chunkStart < $fileSize) {
$chunkEnd = min($chunkStart + $chunkSizeBytes, $fileSize - 1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('https://www.googleapis.com/drive/v3/files/%s?alt=media', $id));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $http->getAccessToken(),
'Range: bytes=' . $chunkStart . '-' . $chunkEnd
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) use ($fp) {
fwrite($fp, $data);
return strlen($data);
});
curl_multi_add_handle($multiHandle, $ch);
$chunkStart = $chunkEnd + 1;
}
// Paralel istekleri gerçekleştirme
do {
$status = curl_multi_exec($multiHandle, $active);
if ($active) {
curl_multi_select($multiHandle);
}
} while ($active && $status == CURLM_OK);
// Temizlik
curl_multi_close($multiHandle);
fclose($fp);
echo $dosya_adi . " <b>[İNDİRİLDİ]</b><br />";
}
}
}
}
} else {
echo "Kaynak ve indirilecek dizin seçilmelidir";
}
I am using Google Drive IP “Service” account and no user confirmation is required
The connection code is located in the file google_drive_setup.pnp
on the download page include 'google_drive_setup.php';
I’am adding.
function getClient() {
$client = new \Google\Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setScopes(\Google\Service\Drive::DRIVE);
$client->setAuthConfig(AUTHCONFIGPATH);
$client->setAccessType('offline');
return $client;
}
$client = getClient();
$service = new \Google\Service\Drive($client);