Example 2 - Submit to Google Spreadsheet and Upload Attachment to Google Docs
This form will submit the form values to a spreadsheet and also upload a file to google docs
//----------------------------------------------------
// Google Form Sandwich
// Created by Jaz Witham (Jazzerup)
// 2011
// http://www.jazzerup.com
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
// Script uses the Google API to save values of a form into
// a Google Spreadsheet and uploads file 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 = "yourpassword";
// Google Spreadsheet ID (You can get it from the URL when you view the spreadsheet)
$spreadsheetKey = "0AqPXsH_13gn4dEJCc3VieGFkbWNsUGNMSkdFbVFLdEE";
// od6 is the first worksheet in the spreadsheet
$worksheetID="od6";
// Redirect to your thank you page
$thankyou = "../thankyou.html";
//-----------------------------------------------------------
// No need to edit below unless you're a hacker =)
//-----------------------------------------------------------
// 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 ourselves with Google Docs and create a Zend_Gdata_Spreadsheets object.
$authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($user, $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
// 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 certain files
// csv, txt, doc, docx, xls, xlsx,ppt, pptx
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $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" ) ;