3

ファイルアップロードまたはGoogleファイルピッカーのいずれかを使用してhtmlフォームからファイルをアップロードし、ファイルのURLをmysqlデータベースに保存する方法は?

ファイルのアップロードを介してファイルをアップロードし、それを以下のコードの $file 変数に渡そうとしましたが、ファイルはアップロードされていますが、Google ドライブでは無効ですか?

    <?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$myfile=$_REQUEST['file'];

$type=mime_content_type($myfile);

$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My New document');
$file->setDescription('A test document');
$file->setMimeType($type);

$data = file_get_contents($myfile);

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

print_r($createdFile);

?>
4

1 に答える 1

3

$myfile=$_FILES['file']['tmp_name'];の代わりに使用$myfile=$_REQUEST['file'];

「trim(fgets(STDIN));」を使用します これは html ページからは機能しません。

$_GET['code']Google が許可されたユーザーをサイトにリダイレクトするときに、アプリは認証コードを読み取る必要があります。

これは、以下に示すコードでテストできます。テストするには、コードを として保存し、index.phpで利用できるようにする必要がありますhttp://localhost/setRedirectUriメイン ドメインへのリダイレクトのみを許可します (http://localhost/test.phpは許可されません)。$_GET['code']が空の場合、accounts.google.com にリダイレクトされます。アプリを許可すると、 に返送されhttp://localhost/?code={your authcode}ます。認証コードを保存して、他のページでも使用できるようになりました。

このテスト コードでは、ファイル アップロード フォームが送信され http://localhost/?code={your authcode}、action 属性が空になります。

print_r($createdFile);ファイルの URL をデータベースに保存するための情報が表示されます。

最初に資格情報を取得するために Google API コンソール プロジェクトをセットアップすることを忘れないでください。http://enarion.net/programming/php/google-client-api/google-client-api-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('**********.apps.googleusercontent.com');
$client->setClientSecret('********************');
$client->setRedirectUri('http://localhost/');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

if(empty($_GET['code']))
{
    $client->authenticate();
}
?>
<form method="post" enctype="multipart/form-data" action="">
<input type="file" name="file">
<input type="submit" value="verzenden">
</form>
<?


if(!empty($_FILES['file']['tmp_name']))
{

$myfile=$_FILES['file']['tmp_name'];

$type=mime_content_type($myfile);



$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('My New document');
$file->setDescription('A test document');
$file->setMimeType($type);

$data = file_get_contents($myfile);

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

print_r($createdFile);
}
于 2013-04-20T13:41:54.697 に答える