如何使用.php文件中指定的变量加载.phtml文件

时间:2017-07-27 07:10:54

标签: php google-drive-api

某些变量用于phtml文件,不能在php文件中使用。我也尝试使用GLOBAL变量范围,但它对我没有帮助。 单击“提交”按钮后,我需要执行display()函数。在功能显示()中,我写了一个条件,点击“提交”按钮后执行,然后我想收到下载文件的ID,用于在提交按钮下面生成下载链接。 的错误:

Undefined index: $fileId in C:\wamp64\www\index.phtml on line 24

我的代码: PHTML文件

<?php global $fileId; ?>
<body>
   <?php 
        function displayID()
        {   
            //$fileId = '5';
            if($fileId ?? TRUE) 
            {
                echo $fileId;
            } else { echo 'Not working'; }
        }            
    ?>

    <div style="border-bottom: 2px solid black; border-top: 2px solid black; background-color: white; width: 25%; text-align: center; display: block; margin: auto; ">
        <?php foreach ($files as $file) { ?>
            <p style="text-align: inherit"><?php echo $file; ?></p>
            <?php  ?>
        <?php } ?>
    </div>

    <div style="text-align: center; float: inherit; margin-top: 5%">
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
            <input type="submit" value="Upload" name="submit"><br><br>
        </form>
        <form method="GET" action="<?php echo $_SERVER['PHP_SELF']?>">
            <input type="submit" value="Get Link" name="Get"><br><br>
        </form>
        <br>
        <div style="border-bottom: 2px solid black; border-top: 2px solid black; background-color: white; width: 25%; text-align: center; display: block; margin: auto; ">
            <p> 
            <?php 
                if (isset($_GET['Get']))
                    {
                       displayID();
                    } else {
                        echo '';
                    }
            ?> 
            </p>
        </div>
    </div>

</body>

PHP-文件:

    <?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) {
        $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';
?>

0 个答案:

没有答案