Hello,
Using the code written by @katsry to create sub-directories in Google Drive, I upload directories to Google Drive, including sub-directories and files, with the following codes:
Upload the file to the specified directory
function insertFile($service, $parentId, $filename) {
$file = new Google_Service_Drive_DriveFile();
$file->setName(basename($filename));
$file->setMimeType(mime_content_type($filename));
$parentId = !empty($parentId) ? $parentId : null;
$file->setParents(array($parentId));
try {
$data = file_get_contents($filename);
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => mime_content_type($filename),
));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
return $createdFile->getName();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
Function to check whether the directory or file uploaded in Google Drive exists
function dir_exists($fileid, $service) {
$folderId = $fileid;
$results = $service->files->listFiles(array(
'q' => "'$folderId' in parents"
));
$klasorler_dizi = [];
foreach ($results->getFiles() as $file) {
$klasorler_dizi[$file->getId()] = $file->getName();
}
return $klasorler_dizi;
}
Directory creation and file upload function in Google Drive
function createDirectoryPath($service, $source_selected, $parentId = 'root') {
// If there is BACKUPDIR in the file path, remove it. So that "../", "./", "BACKUPDIR" does not create the directory on the remote server
$source = str_replace(array('../', './', BACKUPDIR.'/'), '', $source_selected);
$directories = explode('/', $source);
foreach ($directories as $directory) {
if($directory == end($directories) && !is_dir($source_selected))
{
$dizinveyadosyavarmi = dir_exists($parentId, $service);
if(!in_array($directory, $dizinveyadosyavarmi))
{
insertFile($service, $parentId, $source_selected);
}
}
else
{
$dizinveyadosyavarmi = dir_exists($parentId, $service);
if(in_array($directory, $dizinveyadosyavarmi))
{
$dizinveyadosyavarmi = array_flip($dizinveyadosyavarmi);
$parentId = $dizinveyadosyavarmi[$directory];
}
else
{
$parentId = createSubdirectory($service, $parentId, $directory);
}
}
}
return $parentId; // ID of the last folder created
}
Only folder creation function in Google Drive
function createSubdirectory($service, $parentId, $subdirectoryName) {
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $subdirectoryName,
'mimeType' => 'application/vnd.google-apps.folder',
'parents' => $parentId ? array($parentId) : null
));
try {
$folder = $service->files->create($fileMetadata, array('fields' => 'id'));
//printf("Klasör ID: %s\n", $folder->id);
return $folder->id;
} catch (Exception $e) {
//echo "Bir hata oluştu: " . $e->getMessage();
}
return null;
}
Code to send to above functions to upload selected files or directories to google drive
$path = ltrim(rtrim($_POST['local_selected_folder_or_file'],'/'),'/');
$rootId = $_POST['selected_directory_in_google_drive'];
if(is_dir($path)){
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $file => $object){
if (substr($file, -1) != '.' && substr($file, -2) != '..')
{
$file = str_replace(array('\\','\\\\','//'), '/', $file);
createDirectoryPath($service, $file, $rootId);
}
}
echo "<strong>".str_replace(BACKUPDIR.'/', '', $path) . "</strong> Directory Successfully Uploaded to Google Drive";
}else{
if(createDirectoryPath($service, $path, $rootId))
{
echo "<strong>".basename($path) . "</strong> File Successfully Uploaded to Google Drive";
}
}
My question is:
How to download selected directory from Google Drive including sub-directories and files?