dtye7921 2016-01-20 07:30
浏览 62
已采纳

未定义的属性:尝试将文件插入云端硬盘时的Google_Http_Request :: $ id

I'm trying to insert a user-submitted file into a specific folder named after the user, using yannisg's Google Uploader as base and a modified version of Google Drive example. The folder is created during the upload process. But I'm getting the above error about undefined id. Then it uploads the file into Drive's root folder. I'm using the v1-master of the PHP client.

function upload(){
    $client = $this->client;
    $service = $this->service; //$service = new Google_Service_Drive($client);

    //Create folder
    $folder = new Google_Service_Drive_DriveFile(array(
        'mimeType' => 'application/vnd.google-apps.folder'));
    $folder->setTitle($this->ownerName); //applicant's name
    $folderData = $service->files->insert($folder, array(
        'fields' => 'id'));

    //create file
    $file = new Google_Service_Drive_DriveFile();
    $file->title = $this->fileName;
    $chunkSizeBytes = 1 * 1024 * 1024;

    $fileRequest = $this->fileRequest;
    $mimeType = $this->mimeType;

    //Set parent id
    $parent = new Google_Service_Drive_ParentReference();
    $parent->setId($folderData->id);
    $file->setParents(array($parent));

    $request = $service->files->insert($file);

    //The rest of the script...
}

I think the problem lies here

$parent->setId($folderData->id);

Here's the same portion from the modified example script that works nicely. I would've have used this but I can't seem to properly integrate it into another script I need to run. And yannisg's have the refresh token taken care of.

//Create folder   
$folder = new Google_Service_Drive_DriveFile(array(
    'mimeType' => 'application/vnd.google-apps.folder'));
$folder->setTitle($_FILES["uploaded"]["name"]); //applicant's name
$folderData = $service->files->insert($folder, array(
    'fields' => 'id'));

var_dump($folderData);

//Create file
$file = new Google_Service_Drive_DriveFile();
$file->title = $_FILES["uploaded"]["name"];
$chunkSizeBytes = 1 * 1024 * 1024;

//Set parent id
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId($folderData->id);
$file->setParents(array($parent));

Var dumping the second script gives me object(Google_Service_Drive_DriveFile) while the first gave object(Google_Http_Request). Is it because the first one is in a class?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doucheyi1347 2016-01-25 03:30
    关注

    I solved it. It was related to deferring. In yannisg's script,

    $client->setDefer(true);
    

    is declared in an earlier function, which means the statement for creating the folder are executed together with the file. No wonder it couldn't grab the parents id. After moving it below the folder insertion part, everything works properly once again.

    //folder insertion    
    
    $client->setDefer(true);
    $request = $service->files->insert($file); //file insertion
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?