2013-08-21 14 views
7

Używam apletu google do przesyłania plików, To jest mój kod do tej pory teraz plik został przesłany do mojego katalogu głównego i chcę zmienić ścieżkę do innego folderu,Przesyłanie pliku do określonego folderu przy użyciu apletu google drive api z PHP

<?php 
     error_reporting(E_ALL); 
     ini_set('display_errors', 1); 
     require_once 'google-api-php-client/src/Google_Client.php'; 
     require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; 
     $client = new Google_Client(); 
     // Get your credentials from the APIs Console 
     $client->setClientId('***'); 
     $client->setClientSecret('***'); 
     $client->setRedirectUri('http://***'); 
     $client->setScopes(array('https://www.googleapis.com/auth/drive')); 

     if(empty($_GET['code'])) 
     { 
      $client->authenticate(); 
     } 

     $myfile= "video.mp4"; 
     $type='video/mp4'; 

     $service = new Google_DriveService($client); 

     // Exchange authorization code for access token 
     $accessToken = $client->authenticate($_GET['code']); 
     $client->setAccessToken($accessToken); 


     //Insert a file 

     $file = new Google_DriveFile(); 
     $file->setTitle('filename.mp4'); 
     $file->setDescription('A video'); 
     $file->setMimeType($type); 

     $data = file_get_contents($myfile); 

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

     print_r($createdFile); 
     echo "<br />"; 

próbowałem kilka posty z stackoverflow i żaden z nich nie pracował dla mnie mam błąd, góry dzięki

Odpowiedz

4

Ustaw folder nadrzędny tak:

//Set the Parent Folder 
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference 
$parent->setId('0B9mYBlahBlahBlah'); 
$file->setParents(array($parent)); 
+0

Nowa klasa nosi nazwę 'Google_Service_Drive_ParentReference' – Moak

0

Użyj poniższego kodu. Działa idealnie, aby wstawić plik na dysku google.

<?php 
require_once 'Google/autoload.php'; 
require_once 'Google/Client.php'; 
require_once 'Google/Service/Drive.php'; 

$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId('your client id'); 
$client->setClientSecret('your client secret'); 
$client->setRedirectUri('redirect uri set on google apps'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive.file')); 

session_start(); 

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) { 

    if (isset($_GET['code'])) { 
     $client->authenticate($_GET['code']); 
     $_SESSION['access_token'] = $client->getAccessToken(); 
    } 
    else 
     $client->setAccessToken($_SESSION['access_token']); 

    $service = new Google_Service_Drive($client); 

    //Insert a file 
    $file = new Google_Service_Drive_DriveFile(); 
    $file->setTitle(uniqid().'.jpg'); 
    $file->setDescription('A test document'); 
    $file->setMimeType('image/jpeg'); 

    $data = file_get_contents('a.jpg'); 

    $createdFile = $service->files->insert($file, array(
     'data' => $data, 
     'mimeType' => 'image/jpeg', 
     'uploadType' => 'multipart' 
    )); 

    print_r($createdFile); 
} 
else { 
    $authUrl = $client->createAuthUrl(); 
    header('Location: ' . $authUrl); 
    exit(); 
} 
?> 
2

Wygląda na to, że klasa Google_Service_Drive_ParentReference() nie działa. Jednak działa następujący kod:

$fileMetadata = new Google_Service_Drive_DriveFile(array(
     //Set the Random Filename 
     'name' => $rdmName, 
     //Set the Parent Folder 
     'parents' => array('0BzaRxkCDivjIM1pUYU1SLWVxOGM') // this is the folder id 
    )); 

    try{ 

     $newFile = $drive_service->files->create(
      $fileMetadata, 
      array(
       'data' => file_get_contents(TESTFILE), 
       'mimeType' => 'application/pdf', 
       'uploadType' => 'media' 
      ) 
     ); 


    } catch(Exception $e){ 

     echo 'An error ocurred : ' . $e->getMessage(); 

    } 
Powiązane problemy