2014-05-04 24 views
6

Vì vậy, tôi đã gặp sự cố lạ với việc tải lên PHP bằng GAPI. Các tập tin thực sự được tạo ra trên ổ đĩa nhưng đối với một số lý do dữ liệu không làm cho nó vào Google và nó chỉ tạo ra một tập tin với 0 byte.Tệp API trống của Google Drive PHP API

Dưới đây là mã của tôi:

function uploadFile($service, $title, $description, $parentId, $mimeType, $filepath) { 
    $mimeType = "image/png"; 
    $title = "test.png"; 
    $file = new Google_Service_Drive_DriveFile(); 
    $file->setTitle($title); 
    $file->setDescription($description); 
    $file->setMimeType($mimeType); 

    // Set the parent folder. 
    if ($parentId != null) { 
     $parent = new Google_Service_Drive_ParentReference(); 
     $parent->setId($parentId); 
     $file->setParents(array($parent)); 
    } 

    try { 
     $data = file_get_contents(); 

     $createdFile = $service->files->insert($file, array(
      'data' => $data, 
      'mimeType' => $mimeType, 
     )); 

     // Uncomment the following line to print the File ID 
     // print 'File ID: %s' % $createdFile->getId(); 

     //return $createdFile; 
    } catch (Exception $e) { 
     echo "An error occurred: " . $e->getMessage(); 
    } 
} 

Tất cả mọi thứ được xác thực nên tôi biết đó không phải là vấn đề. Khi tôi ra $ dữ liệu, tôi nhận được mess của tào lao mà bạn thường nhận được khi kéo một file vì vậy tôi biết đó không phải là vấn đề .. Tất cả các phạm vi nên được quyền nhưng ở đây họ anyways:

$client->addScope("https://www.googleapis.com/auth/drive"); 
$client->addScope("https://www.googleapis.com/auth/drive.file"); 
$client->addScope("https://www.googleapis.com/auth/drive.appdata"); 
$client->addScope("https://www.googleapis.com/auth/drive.scripts"); 
$client->addScope("https://www.googleapis.com/auth/drive.apps.readonly"); 
$client->addScope("https://www.googleapis.com/auth/drive.metadata.readonly"); 
$client->addScope("https://www.googleapis.com/auth/drive.readonly"); 

Không tài liệu hướng dẫn tôi có thể tìm thấy về vấn đề này vì vậy bất kỳ trợ giúp sẽ được thực sự đánh giá cao!

Trả lời

6

Tôi có thể tìm ra điều này và muốn để điều này cho bất kỳ ai khác có thể gặp phải sự cố này. Đã kết thúc tìm kiếm thông qua mã nguồn và nhận thấy một câu lệnh If không bị sa thải.

Thay đổi

$createdFile = $service->files->insert($file, array(
    'data' => $data, 
    'mimeType' => $mimeType, 
)); 

Để

$createdFile = $service->files->insert($file, array(
    'data' => $data, 
    'mimeType' => $mimeType, 
    'uploadType' => 'media' //add this for pdfs to work 
)); 

Nó chỉ là dễ dàng! Ghét nó khi nó dễ dàng ..

+1

điều quan trọng nhất là 'uploadType' => 'media' –

+1

Hallelujah! Vấn đề với việc bắt đầu với quickstart.php và nhảy ngay vào hoạt động viết - họ đã không thiết lập bạn cho điều đó, không có họ không. Làm cho bạn săn tìm sức mạnh hủy diệt, đó có lẽ là một ý tưởng hay :) –

Các vấn đề liên quan