环境:PHP
使用文件夹和文件: 我在Google驱动器中创建了一个文件夹和该文件夹中的4个子文件夹。
Google DRIVE API
安装Google客户端库。
composer require google/apiclient:^2.0
我与服务帐户共享了these folder and 4 sub folders。
预期结果:文件夹和文件应在子文件夹中创建。
实际结果:
1 day it works properly but in next day 404 error updated in error log.
{ "error": {"errors": [{ "domain": "global","reason": "notFound","message": "File not found: .","locationType": "parameter","location": "fileId"} ],"code": 404, "message": "File not found: ." } }
我的代码:
//======Send To Google Drive======//
function send_google_drive($id,$fileno,$filename1,$filename2){
global $wpdb;
require(ABSPATH.'/wp-content/themes/enemat/googledrives/vendor/autoload.php');
$client = getClient();
$service = new Google_Service_Drive($client);
$folderslink = "";
$docusignorgs = "";
$contractdrivelink = "";
$parentfolders = get_option("parentfolders");
$crmfolders = get_option("crmfolders");
try{
//===Root Folder's Sub Folder======//
$results = $service->files->listFiles();
$parents = "";
foreach ($results->getFiles() as $item) {
if ($item['name'] == $parentfolders) {
$parents = $item['id'];
break;
}
}
$optParams = array(
'pageSize' => 10,
'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
'q' => "'".$parents."' in parents"
);
$results = $service->files->listFiles($optParams);
$parentsarray = array();
foreach ($results->getFiles() as $item) {
$parentsarray[$item['name']] = $item->getId();
}
$parentid = $parentsarray[$crmfolders];
//===Root Folder's Sub Folder======//
//===Sub Folder's Folders======//
$optParams1 = array(
'pageSize' => 10,
'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
'q' => "'".$parentid."' in parents"
);
$results = $service->files->listFiles($optParams1);
//===Sub Folder's Folders======//
//==========CREATE FOLDER====//
$childid = "";
foreach ($results->getFiles() as $item) {
if ($item['name'] == $fileno) {
$childid = $item['id'];
break;
}
}
if(empty($childid)){
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $fileno,
'parents'=>array($parentid),
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array(
'fields' => 'id'));
$folderId = $file->id;
}else{
$folderId = $childid;
}
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($folderId, $newPermission);
//==========CREATE FOLDER====//
if(!empty($filename1)){
try{
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => "OFFRE DE PRIME - ".$fileno."",
'parents' => array($folderId)
));
$content = file_get_contents($filename1);
$files = $service->files->create($fileMetadata, array(
'data' => $content,
'uploadType' => 'resumable',
'fields' => 'id'));
$fileids = $files->id;
$docusignorgs = "https://drive.google.com/open?id=".$fileids."";
$folderslink = "https://drive.google.com/drive/folders/".$folderId."";
@unlink(ABSPATH."wp-content/themes/enemat/pdfs/".basename($filename1));
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($fileids, $newPermission);
}catch (Google_ServiceException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Google_IOException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
} catch (Google_Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}
}
if(!empty($filename2)){
try{
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => "contrat projet - ".$fileno."",
'parents' => array($folderId)
));
$content = file_get_contents($filename2);
$files = $service->files->create($fileMetadata, array(
'data' => $content,
'uploadType' => 'resumable',
'fields' => 'id'));
$fileids1 = $files->id;
$contractdrivelink = "https://drive.google.com/open?id=".$fileids1."";
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($fileids1, $newPermission);
@unlink(ABSPATH."wp-content/themes/enemat/pdfs/".basename($filename2));
}catch (Google_ServiceException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Google_IOException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
} catch (Google_Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}
}
$SQLUPDATE = "UPDATE wp_clientinfo SET docusignorgs='".$docusignorgs."',folderid='".$folderslink."',contractdrivelink='".$contractdrivelink."' WHERE id='".$id."'";
$wpdb->query($SQLUPDATE);
}catch (Google_ServiceException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Google_IOException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
} catch (Google_Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}
}
//======Send To Google Drive======//