使用google drive php api上载的文件包含空内容

使用google drive php api上载的文件包含空内容,php,google-api,google-api-php-client,google-api-client,google-drive-api,Php,Google Api,Google Api Php Client,Google Api Client,Google Drive Api,下面是代码。使用google drive php api在google drive上成功上传/创建/插入文件,但上传文件后其内容为空 session_start(); set_include_path("/src/" . PATH_SEPARATOR . get_include_path()); require_once 'Google/Client.php'; require_once 'Google/Service/Drive.php'; $client_id = 'XXXXXX'; $c

下面是代码。使用google drive php api在google drive上成功上传/创建/插入文件,但上传文件后其内容为空

session_start();
set_include_path("/src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';

$client_id = 'XXXXXX';
$client_secret = 'XXXXXX';
$redirect_uri = 'XXXXXX';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");

$service = new Google_Service_Drive($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['upload_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['upload_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
  $client->setAccessToken($_SESSION['upload_token']);
  if ($client->isAccessTokenExpired()) {
    unset($_SESSION['upload_token']);
  }
} else {
  $authUrl = $client->createAuthUrl();
}

$filename = "file.csv";

echo "<BR><BR>stage 1";

if ($client->getAccessToken())
{       
    $data = file_get_contents($filename);

    if (isset($data) && $data)
    {
        $file = new Google_Service_Drive_DriveFile();
        echo "<BR><BR>stage 2";
        if (isset($file))
        {            
            $file -> setTitle("gsexpo");
            $file -> setMimeType("application/vnd.google-apps.spreadsheet");

            if (isset($file -> parentId) && $file -> parentId != null) {
                $parent = new Google_ParentReference();
                $parent -> setId($file -> parentId);
                $file -> setParents(array($parent));
            }

            echo "<BR><BR>stage 3";

            $result2 = $service->files->insert(
                $file,
                array(
                  'data' => $data,
                  'mimeType'   => "application/vnd.google-apps.spreadsheet",                  
                  'convert' => true
                ));

            echo "Result: ";            
            var_dump($result2->title);
        }
        else
        {
            echo "No file object";
        }
    }
    else
    {
        echo "File read failed";    
    }
}
if (isset($authUrl)) 
{     echo "<a href='".$authUrl."'>Connect Me!</a>";    }
这里是我的GoogleAPI php客户端库的链接,我正在使用它来实现这个目的


您需要将“uploadType”参数添加到插入请求数组中。例如:

$result2 = $service->files->insert(
    $file,
    array(
         'data' => $data,
         'mimeType'   => "application/vnd.google-apps.spreadsheet",                  
         'convert' => true,
         'uploadType' => 'media',
    )
);

只需使用以下代码:

$result2 = $service->files->create($file, array(
            'data' => $data,
            'mimeType' => 'application/vnd.google-apps.spreadsheet',
            'uploadType' => 'multipart'
        ));
如果有任何问题,请告诉我