Show
Drive API allows to interact with Google Drive storage and manage files/folders programmatically. We can access or manage files in Google Drive from website using this REST API. The Google Drive API supports many popular programming languages and PHP is one of them. In PHP, the cURL service can be used to make HTTP requests to Drive REST API. We have already shared the tutorials to upload files to Google Drive and download files from Google Drive using PHP. In this tutorial, we will explain how to delete files from Google Drive via REST API using PHP. In this example script, we will implement the following functionality to delete files from Google Drive with PHP.
Before getting started to build a PHP script to delete files from Google drive using PHP, take a look at the file structure. google_drive_file_delete_with_php/ ├── config.php ├── index.php ├── google_drive_sync.php ├── GoogleDriveApi.class.php └── css/ └── style.css Create Google Project and Enable Drive APIThe Client ID and Client Secret are required to make API calls to Google Drive. If you already have an existing project at the Google API console, create OAuth Client IDs from there. But you have to make sure that Google Drive API is enabled in this existing Google API console application. If you don’t have any application at the Google API console, follow the below steps to register your application and create OAuth Client IDs on Google Developers Console.
A dialog box will appear with OAuth client details, note the Client ID and Client secret for later use in the script. This Client ID and Client secret allow you to access the Google Drive API. Note that: The Client ID and Client secret need to be specified at the time of the Google Drive API call. Also, the Authorized redirect URIs must be matched with the Redirect URL specified in the script. Google Drive API Handler PHP LibraryGoogle provides a PHP client library to make Drive API calls, but it contains many additional services that come with a huge number of files and are large in size. To make the process simple, we will build a custom library to handle the Google Drive API calls using PHP. This Google Drive API class helps to authenticate with a Google account and access the Drive API using PHP cURL. This custom PHP library will use Google Drive API v3 to handle authentication and file delete processes.
<?php Google API Configuration (config.php)In the Google API constants:
Google OAuth URL:
<?php // Google API configuration Note that: The Client ID and Client Secret can be found on the Google API Manager page of the API Console project. File ID Input HTML Form (index.php)Create an HTML form to allow the user to input the ID of the Google Drive file that wanted to delete.
<?php Delete File from Google Drive (google_drive_sync.php)The
When the user redirects back after the authentication with their Google account.
<?php Note that: This file URL must be set as Redirect URL in Authorized redirect URIs section of the Google API console project. Important Notes:If you want to delete the file from Google Drive other than the authenticated Google account, make sure the file sharing option is set to Anyone with the link with Editor permission. Checklist for Testing:Google Application
Verification: In the development mode, you can test it by adding Test Users in the OAuth consent screen of the Google application.
Add Event to Google Calendar using PHP ConclusionDelete files from Google drive with REST API is an easy way to manage files from the website. You can delete files from Google Drive dynamically using PHP. Since the access token is stored in SESSION, the file delete request can execute without Google account authentication and files can be removed from the Google Drive account. Are you want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request If you have any questions about this script, submit it to our QA community - Ask Question How do I get a list of all files in Google Drive?Call the listFiles method with the Drive folder name and it will create a list of all files and appends them to a spreadsheet.
How get file from Google Drive in PHP?Create Google Project and Enable Drive API. ... . Create Database Table. ... . Google Drive API Handler PHP Library. ... . Database and API Configuration (config. ... . Database Connection (dbConfig. ... . File ID Input Form (index. ... . Download File from Google Drive (google_drive_sync. ... . Downloaded File Details (details.. Does Google Drive support PHP?Google Drive API PHP Library
Google provides a PHP client library to make Drive API calls, but it contains many additional services that come with a huge number of files and are large in size. To make the process simple, we will build a custom library to handle the Google Drive API calls with PHP.
Is Pydrive deprecated?Deprecated. This project is deprecated and no longer maintained. No further changes will be made.
|