使用未定义的常量文件 - 假设'文件'

时间:2017-07-27 05:06:27

标签: php google-drive-api

我想写下载文件的变量ID,但在点击按钮下载后,我有一堆错误:

  • 使用未定义的常量文件 - 假设'文件'在 第39行的C:\ wamp64 \ www \ index.php;
  • 未捕获错误:调用未定义函数getId() 第39行的C:\ wamp64 \ www \ index.php
  • 在线上调用C:\ wamp64 \ www \ index.php中的未定义函数getId() 39

我的代码:

    <?php
    session_start();
    $url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    $url = $url_array[0];
    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();
    $client->setClientId('xxx');
    $client->setClientSecret('xxx');
    $client->setRedirectUri($url);
    $client->setScopes(array('https://www.googleapis.com/auth/drive'));
    if (isset($_GET['code'])) {
        $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
        header('location:'.$url);exit;
    } elseif (!isset($_SESSION['accessToken'])) {
        $client->authenticate();
    }
    $fileId = '';
    $files = array();
    $dir = dir('files');
    while ($file = $dir->read()) {
        if ($file != '.' && $file != '..') {
            $files[] = $file;
        }
    }
    $dir->close();
    if (!empty($_POST)) {
        $client->setAccessToken($_SESSION['accessToken']);
        $service = new Google_DriveService($client);
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $file = new Google_DriveFile();
        foreach ($files as $file_name) {
            $fileId = "";
            $file_path = 'files/'.$file_name;
            $mime_type = finfo_file($finfo, $file_path);
            $file->setTitle($file_name);
            $file->setDescription('This is a '.$mime_type.' document');
            $file->setMimeType($mime_type);
            $fileId = file.getId();
            $service->files->insert(
                $file,
                array(
                    'data' => file_get_contents($file_path),
                    'mimeType' => $mime_type,
                    'fields' => 'id'
                )
            );
        }
        finfo_close($finfo);
        header('location:'.$url);exit;
    }
    include 'index.phtml'; ?>

1 个答案:

答案 0 :(得分:0)

工作代码,感谢@ bhill77:

<?php
    session_start();
    $url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    $url = $url_array[0];
    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();
    $client->setClientId('xxx');
    $client->setClientSecret('xxx');
    $client->setRedirectUri($url);
    $client->setScopes(array('https://www.googleapis.com/auth/drive'));
    if (isset($_GET['code'])) {
        $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
        header('location:'.$url);exit;
    } elseif (!isset($_SESSION['accessToken'])) {
        $client->authenticate();
    }
    $fileId = '';
    $files = array();
    $dir = dir('files');
    while ($file = $dir->read()) {
        if ($file != '.' && $file != '..') {
            $files[] = $file;
        }
    }
    $dir->close();
    if (!empty($_POST)) {
        $client->setAccessToken($_SESSION['accessToken']);
        $service = new Google_DriveService($client);
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $file = new Google_DriveFile();
        foreach ($files as $file_name) {
            $fileId = "";
            $file_path = 'files/'.$file_name;
            $mime_type = finfo_file($finfo, $file_path);
            $file->setTitle($file_name);
            $file->setDescription('This is a '.$mime_type.' document');
            $file->setMimeType($mime_type);
            $fileId = $file->getId();
            $service->files->insert(
                $file,
                array(
                    'data' => file_get_contents($file_path),
                    'mimeType' => $mime_type,
                    'fields' => 'id'
                )
            );
        }
        finfo_close($finfo);
        header('location:'.$url);exit;
    }
    include 'index.phtml'; ?>