2014-05-04 6 views
6

तो मुझे गैपी के साथ PHP अपलोड के साथ अजीब समस्याएं आ रही हैं। फ़ाइल वास्तव में ड्राइव पर बनाई जाती है लेकिन किसी कारण से डेटा Google को नहीं बनाता है और यह सिर्फ 0 बाइट्स वाली फ़ाइल बनाता है।Google ड्राइव PHP API खाली अपलोड की गई फ़ाइल

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(); 
    } 
} 

सब कुछ प्रमाणीकृत है तो मुझे पता है कि समस्या नहीं है:

यहाँ मेरी कोड है। जब मैं उत्पादन $ डेटा, मैं बकवास है कि आप आमतौर पर जब किसी फ़ाइल खींच पाने की गंदगी मिल इसलिए मुझे पता है कि इस मुद्दे को .. स्कोप के ठीक होना चाहिए, लेकिन यहाँ वे वैसे भी कर रहे हैं नहीं है:

$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"); 

नहीं प्रलेखन मैं इस समस्या पर पा सकता हूं इसलिए किसी भी मदद की वास्तव में सराहना की जाएगी!

उत्तर

6

मैं इसे समझने में सक्षम था और इसे किसी और के लिए छोड़ना चाहता था जिसमें यह समस्या हो सकती है। समाप्त हो गया स्रोत स्रोत के माध्यम से देख रहा है और एक अगर कथन जो निकाल नहीं दिया गया था देखा।

बदलें

$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 
)); 

यह सिर्फ इतना है कि आसान है! यह आसान है जब यह आसान है ..

+1

सबसे महत्वपूर्ण बात यह है कि 'अपलोडटाइप' => 'मीडिया' –

+1

हैललुजाह! Quickstart.php से शुरू करने और लिखने के संचालन में सीधे कूदने में समस्या - उन्होंने आपको इसके लिए सेट नहीं किया, नहीं, उन्होंने नहीं किया। आप विनाशकारी शक्तियों के लिए शिकार करते हैं, जो शायद एक अच्छा विचार है :) –

संबंधित मुद्दे