【问题标题】:Does the PHP file_exists function work in Google App Engine?PHP file_exists 函数在 Google App Engine 中是否有效?
【发布时间】:2015-06-10 11:33:25
【问题描述】:

当我将 php 文件部署到 GAE 时,file_exists 函数不起作用。如果我使用 Google App Engine Launcher 从我的计算机本地测试完全相同的代码,它就可以工作。这是 PHP 和 app.yaml 文件

indexTest.php

<?php
DEFINE("TESTFILE", 'testfile.txt');
echo TESTFILE.'<br>';

$doesTheFileExist = file_exists(TESTFILE);
echo '$doesTheFileExist: ' . $doesTheFileExist. '<br>';

if (!file_exists(TESTFILE)) {
  echo 'I guess it doesnt exist';
}

RETURN;

app.yaml

application: application-name
version: 1
runtime: php55
api_version: 1
threadsafe: true

handlers:

- url: /favicon.ico
  static_files: favicon.ico
  upload: favicon.ico

- url: /google-api-php-client
  script: google-api-php-client

- url: /testfile.txt
  static_files: testfile.txt
  upload: testfile.txt

- url: /
  script: indexTest.php

本地输出

GAE 上的输出

为什么在本地测试的时候可以,部署后就不行了?

我尝试更改文件的位置并更改 app.yaml 文件,并添加如下内容:

- url: /templates
  static_dir: templates

并将文件放在 templates 目录中。但同样,它在本地主机中工作,但未部署到 App Engine。

【问题讨论】:

  • 文件在哪里?
  • 位于根目录。

标签: php google-app-engine app.yaml


【解决方案1】:

如果您希望能够从应用中读取静态资源,则需要在处理程序上设置 application_readable 标志。

当然,如果您实际上并不希望能够将文件作为静态资源提供,而只是希望能够读取它,那么甚至不要将它包含在您的 app.yaml 文件中,它将是已上传并可从您的应用访问。

请参阅文档here

【讨论】:

  • 我正在测试对 Google 的授权,以及对 Google Drive 的文件创建。 PHP 中的一个示例使用文件作为在 Drive 中创建新文件的基础。所以,我不提供文件的内容。我可能不会使用该策略,而是创建一个空文件,然后将内容写入其中,或类似的东西。我有点担心让应用程序可读。我立即想到“安全风险”。
  • "application_readable" 意味着您可以静态提供它并且可以从您的应用程序中读取它 - 这是必需的,因为通常静态文件由基础设施提供,而不是部署到与应用程序代码相同的位置。如果您不需要静态提供文件,请不要将其放入 app.yaml 文件中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2013-01-17
相关资源
最近更新 更多