【问题标题】:upload file to Google drive through html form and store url in input-text box to write to mysql?通过html表单将文件上传到Google驱动器并将url存储在输入文本框中以写入mysql?
【发布时间】:2013-04-20 08:36:46
【问题描述】:

如何通过文件上传或谷歌文件选择器通过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);

?>

【问题讨论】:

    标签: php google-drive-api google-api-php-client


    【解决方案1】:

    使用$myfile=$_FILES['file']['tmp_name']; 而不是$myfile=$_REQUEST['file'];

    你使用'trim(fgets(STDIN));'这不适用于 html 页面。

    当 google 将授权用户重定向回您的网站时,您的应用必须从 $_GET['code'] 读取您的授权码。

    您可以使用下面显示的代码对此进行测试。要对其进行测试,您必须将代码保存为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);
    }
    

    【讨论】:

    • 上传的文件还是无效?
    • print_r($createdFile) 的输出是什么?
    猜你喜欢
    • 2016-10-12
    • 2018-11-08
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多