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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路