function uploadFile($service, $filePath, $fileName, $mimeType, $destinationFolder) { $file = new Google_Service_Drive_DriveFile(); $file->setTitle($fileName); $file->setDescription($fileName); $file->setMimeType($fileName); $parent = new Google_Service_Drive_ParentReference(); $parent->setId(getParentDirectoryId($service, $destinationFolder)); $file->setParents(array($parent)); try { $data = file_get_contents($filePath . "/" . $fileName); $response = $service->files->insert($file, array('data' => $data, 'mimeType' => $mimeType, 'uploadType' => 'multipart')); return $response; } catch (Exception $e) { throw new Exception("Error: " . $e->getMessage()); } }
/** * Uploads files to the Google drive. * @param $service the Google drive service. * @param string $filePath the directory location where to look for the files. * @param string $fileName the name of the file to be uploaded. * @param string $mimeType the mimeType of the file. * @param string $destinationFolder the directory on the Google drive. * @return string the expanded path. */ function gfUploadFile($client, $service, $filePath, $fileName, $mimeType, $destinationFolder) { $file = new Google_Service_Drive_DriveFile(); $file->setTitle($fileName); $file->setDescription($fileName); $file->setMimeType($fileName); if ($destinationFolder != "root") { $parent = new Google_Service_Drive_ParentReference(); $parent->setId(getParentDirectoryId($service, $destinationFolder)); $file->setParents(array($parent)); } $client->setDefer(true); $chunkSizeBytes = 1 * 1024 * 1024; $request = $service->files->insert($file); // Create a media file upload to represent our upload process. $media = new Google_Http_MediaFileUpload($client, $request, $mimeType, null, true, $chunkSizeBytes); $media->setFileSize(filesize($filePath . "/" . $fileName)); // Upload the various chunks. $status will be false until the process is // complete. $status = false; $handle = fopen($filePath . "/" . $fileName, "rb"); while (!$status && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } // The final value of $status will be the data from the API for the object // that has been uploaded. $result = false; if ($status != false) { $result = $status; } fclose($handle); // Reset to the client to execute requests immediately in the future. $client->setDefer(false); return $result; }