Reputation: 704
I'm trying to make a php script that uploads files from server to client's google drive, I have managed to upload files, and even inside a folder that I created using curl, but the problem is that I need to check if the client already has a folder with the specific name, if yes I must get the id and upload the file to that folder's id, here is the the code I used to make the file :
$folder_fields = "{\"title\":\"innitel\",
\"parents\":\"[{}]\",
\"mimeType\":\"application/vnd.google-apps.folder\"}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v2/files");
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $folder_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ARRAY("Authorization: Bearer $access_token",
"Content-Type: application/json"));
$reponse = curl_exec($ch);
This method will create a folder with the same name each time the script runs. Is there a way to do?
Note : I can't use google-api-php-client because the php version of the server is 5.3 which is not supported.
Upvotes: 3
Views: 2482
Reputation: 22286
The approach depends on whether or not you have globally unique folder names. If the answer is yes, just do a files.list q title = 'foo'. If you get an item, there's the existing ID. If you get no items, you need to create it.
On the other hand, if you don't have globally unique folder names, you need to build fetch the folder hierarchy and check parents which is significantly more complicated.
Upvotes: 2