Someone asked me how they can attach files to their Google forms.
- You can use the CURL solution from my google form customization blog entry
- Add an additional field to the form called attachment
- In the googlesandwich.php page add the php code to upload the file. Example php upload tutorial
I decided to use this as an opportunity to walk through how to connect to a google form using the google API instead of the CURL solution. The advantage of the Google API is you do not need to make your form available on the web under the share settings.
I also decided to take advantage of google docs and use it as the location to save my attachment. Unfortunately as of Nov 2011, there is not an easy way to upload arbitrary files. You can only upload csv, txt, doc, xls, ppt files.
Side Note:
I am using Version 1 of the Google API. I tried using a later version which allows you to post arbitrary files by posting via CURL but you will probably get an error:
GDataServiceForbiddenException Files must be uploaded using the resumable upload mechanism
Google wants you to upload your files in small chunks. From what I have read there is no php supported solution for this yet.
Step 1
To get started you will need PHP, Zend and the Gdata Zend Libraries installed on your web server. Google has an excellent tuttorial on how to do it with special scripts to check if your installation was successful.
Google API ZEND PHP Getting Started Tutorial
Here are the highlights:
- Install PHP (run phpinfo to ensure it successfully installed)
- Download Google Zend Library (Zend Gdata Downloads)
- Rename the folder to ZendGdata
- Upload the ZendGdata folder to your website
- Update your php.ini file include path to point to the installation of your ZendGdata library folder.
include_path = ".:/usr/lib/php:/usr/local/lib/php:/yourpath/toyour/ZendGdata/library"
(Note: On Unix paths are colon delimited and on windows they are semicolon delimited)
- Run the php installation checker to ensure your library is setup correctly
Now ASSUMING you have zend and php working then congratulations, the difficult part is done!
Step 2
Create your google form and save the form to your desktop or build an html form from scratch.
Your form fields will need to match your column headers in your google spreadsheet BUT it has to be all lower case and no space.
Example: If your column name is "First Name" then your field name needs to be "firstname"
Have your form action do a post to the "googlesandwich.php" page
Step 3
Copy this code and paste into a googlesandwich.php page. Change the variables at the top to match your information.
//googlesandwich.php
//----------------------------------------------------
// Google Spreadsheets API PHP Tutorial
// Created by Jaz Witham (Jazzerup)
// 2011
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
// Script allows you to connect to a google spreadsheet via
// the google php zend api and upload an attachment to google docs
//----------------------------------------------------
// Gmail email address and password for google spreadsheet
$user = "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it.
";
$pass = "your email password";
// Google Spreadsheet ID (You can get it from the URL when you view the spreadsheet)
$GSheetID = "0AqPXsH_13gn4dGsxbVZpc1JvYWJZd3JjWGxxbXlSSnc";
// od6 is the first worksheet in the spreadsheet
$worksheetID="od6";
//Redirect to your thank you page
$thankyou = "http://www.jazzerup.com/blogexamples/googleform/thankyou.html";
//-------------------You shouldn't have to change anything after this line------------------
// Include the loader and Google API classes for spreadsheets
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_Docs');
//-----------------------------------------------------------
// Part One - Connect to our Google Spreadsheet
//-----------------------------------------------------------
// Authenticate with Google Docs and create a Zend_Gdata_Spreadsheets object.
$authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($email, $pass, $authService);
$spreadsheetService = new Zend_Gdata_Spreadsheets($httpClient);
foreach ($_POST as $var => $value) {
//Populate the form values into an array to save to the google spreadsheet
$FormData[$var] = $value;
}
// Give your file a unique name
$useruploaded = $FormData["yourname"];
$fileToUpload = $_FILES['attachment']['name'];
$dateuploaded = date("ymd_Hi", time());
// The file a unique name will be the user who uploaded, the file name and the date uploaded.
$filenewname = $useruploaded . " - " . $fileToUpload . " - " . $dateuploaded;
// push the attachment to the bottom of the array so it shows up in your spreadsheet
$FormData["attachment"] = $filenewname;
// Save your form data to your Google spreadsheet
$insertedListEntry = $spreadsheetService->insertRow($FormData, $spreadsheetKey, $worksheetId);
//-----------------------------------------------------------
// Part Two - Upload Attached File to Google Docs
//-----------------------------------------------------------
$fileToUploadTemp = $_FILES['attachment']['tmp_name'];
$content_type = $_FILES['attachment']['type'];
//-----------------------------------------------
// Upload File
//-----------------------------------------------
// Upload the file and convert it into a Google Document.
// With this version of the API you can only upload these file types:
// csv, txt, doc, xls, ppt
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($email, $pass, $service);
$docs = new Zend_Gdata_Docs($client);
//Find out the file mimetype
$filenameParts = explode('.', $fileToUpload);
$fileExtension = end($filenameParts);
// Upload the file to google docs
$newDocumentEntry = $docs->uploadFile($fileToUploadTemp, $filenewname, Zend_Gdata_Docs::lookupMimeType($fileExtension), Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI);
//Redirect to your thank you page
header( "Location: $thankyou" ) ;