/** * Create new file and write into it from file pointer. * Return new file path or false on error. * * @param resource $fp file pointer * @param string $dir target dir path * @param string $name file name * @param array $stat file stat (required by some virtual fs) * * @return bool|string * * @author Dmitry (dio) Levashov **/ protected function _save($fp, $path, $name, $stat) { if ($name !== '') { $path .= '/' . $name; } list($parentId, $itemId, $parent) = $this->_gd_splitPath($path); if ($name === '') { $stat['iid'] = $itemId; } if (!$stat || empty($stat['iid'])) { $opts = ['q' => sprintf('trashed=false and "%s" in parents and name="%s"', $parentId, $name), 'fields' => self::FETCHFIELDS_LIST]; $srcFile = $this->_gd_query($opts); $srcFile = empty($srcFile) ? null : $srcFile[0]; } else { $srcFile = $this->_gd_getFile($path); } try { $mode = 'update'; $mime = isset($stat['mime']) ? $stat['mime'] : ''; $file = new Google_Service_Drive_DriveFile(); if ($srcFile) { $mime = $srcFile->getMimeType(); } else { $mode = 'insert'; $file->setName($name); $file->setParents([$parentId]); } if (!$mime) { $mime = self::mimetypeInternalDetect($name); } if ($mime === 'unknown') { $mime = 'application/octet-stream'; } $file->setMimeType($mime); $size = 0; if (isset($stat['size'])) { $size = $stat['size']; } else { $fstat = fstat($fp); if (!empty($fstat['size'])) { $size = $fstat['size']; } } // set chunk size (max: 100MB) $chunkSizeBytes = 100 * 1024 * 1024; if ($size > 0) { $memory = elFinder::getIniBytes('memory_limit'); if ($memory) { $chunkSizeBytes = min([$chunkSizeBytes, intval($memory / 4 / 256) * 256]); } } if ($size > $chunkSizeBytes) { $client = $this->client; // Call the API with the media upload, defer so it doesn't immediately return. $client->setDefer(true); if ($mode === 'insert') { $request = $this->service->files->create($file, ['fields' => self::FETCHFIELDS_GET]); } else { $request = $this->service->files->update($srcFile->getId(), $file, ['fields' => self::FETCHFIELDS_GET]); } // Create a media file upload to represent our upload process. $media = new Google_Http_MediaFileUpload($client, $request, $mime, null, true, $chunkSizeBytes); $media->setFileSize($size); // Upload the various chunks. $status will be false until the process is // complete. $status = false; while (!$status && !feof($fp)) { elFinder::extendTimeLimit(); // read until you get $chunkSizeBytes from TESTFILE // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file // An example of a read buffered file is when reading from a URL $chunk = $this->_gd_readFileChunk($fp, $chunkSizeBytes); $status = $media->nextChunk($chunk); } // The final value of $status will be the data from the API for the object // that has been uploaded. if ($status !== false) { $obj = $status; } $client->setDefer(false); } else { $params = ['data' => stream_get_contents($fp), 'uploadType' => 'media', 'fields' => self::FETCHFIELDS_GET]; if ($mode === 'insert') { $obj = $this->service->files->create($file, $params); } else { $obj = $this->service->files->update($srcFile->getId(), $file, $params); } } if ($obj instanceof Google_Service_Drive_DriveFile) { return $this->_joinPath($parent, $obj->getId()); } else { return false; } } catch (Exception $e) { return $this->setError('GoogleDrive error: ' . $e->getMessage()); } }
/** * Store data to Google Drive File * @param type $filedata File data to store * @param type $name Name of the file */ public function storeFile($filedata = "", $name = "") { //Initiate file $gfile = new Google_Service_Drive_DriveFile(); $gfile->setName($name); try { //Upload the file $result = $this->google_service->files->create($gfile, array('data' => $filedata, 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart')); return $result; } catch (Exception $ex) { echo "<pre>"; print_r($ex->getMessage()); return false; } return false; }
************************************************/ if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) { // We'll setup an empty 1MB file to upload. DEFINE("TESTFILE", 'testfile-small.txt'); if (!file_exists(TESTFILE)) { $fh = fopen(TESTFILE, 'w'); fseek($fh, 1024 * 1024); fwrite($fh, "!", 1); fclose($fh); } // This is uploading a file directly, with no metadata associated. $file = new Google_Service_Drive_DriveFile(); $result = $service->files->create($file, array('data' => file_get_contents(TESTFILE), 'mimeType' => 'application/octet-stream', 'uploadType' => 'media')); // Now lets try and send the metadata as well using multipart! $file = new Google_Service_Drive_DriveFile(); $file->setName("Hello World!"); $result2 = $service->files->create($file, array('data' => file_get_contents(TESTFILE), 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart')); } ?> <div class="box"> <?php if (isset($authUrl)) { ?> <div class="request"> <a class='login' href='<?php echo $authUrl; ?> '>Connect Me!</a> </div> <?php