问题描述
我想通常似乎对其他人有用的(以及我想做的)是:
<img src="/images/logo.png">
或 <img src="images/logo.png">
这两个选项都在本地运行(在 GCP App Engine 上)。
当我在 shell 中使用命令 gcloud app deploy
时,我的理解是它部署了网站。代码不再起作用,找不到图像,也没有报告错误(只有图像应该在的小蓝框)。
我尝试了许多变体,尝试将文件移动到不同的文件夹,并阅读了这两页:
Differences in declaring your root directory in HTML
Pick images of root folder from sub-folder
我的文件结构可以描述为:
HelloWorld
images
logo.png
test
tester.php
trythis.php
index.php
其中HelloWorld、test 和图像 是文件夹。
我实际上使用 GCP 存储来托管和存储我的图像,我遇到的问题是超链接、锚标记以及从 index.php 到 test.php 的链接。链接找不到文件,所以是同样的问题。
编辑:完整代码
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
<a href="https://www.google.co.uk">Click here</a>
<a href="test/tester.php">Click here</a>
<a href="/test/tester.php">Click here</a>
<a href="trythis.php">Click here</a>
<a href="/trythis.php">Click here</a>
<br>
<img src="images/logo.png">
<img src="/images/logo.png">
</body>
</html>
还有我的 app.yaml:
runtime: php72
所有都在本地运行,在服务器上都不起作用。
解决方法
为了进入图像文件夹,您必须首先进入 HelloWord 文件夹。请尝试以下操作:
<img src="../images/logo.png" >
,
您需要设置包含您的目录文件夹和图像类型的 app.yaml,以便 App Engine 提供这些文件。尝试使用此 app.yaml 进行部署:
runtime: php72
handlers:
# Serve a directory as a static resource.
- url: /images
static_dir: images
- url: /test
static_dir: test
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
static_files: \1
upload: .+\.(gif|png|jpg)$
以井号 (#) 字符开头的行将被忽略或者这是注释。要了解有关 app.yaml 的更多信息,请访问此 link。
对于 PHP 文件,要处理所有请求路由,您需要添加一个前端控制器。您可以在 index.php
链接下面的示例中指定它或为前端控制器创建一个新文件,但请确保您将在 entrypoint: serve path/to/my/front/controller.php
中添加此行 app.yaml
。
index.php:
<?php
switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {
case '/':
require 'index.php';
break;
case 'trythis.php':
require 'trythis.php';
break;
case 'test/tester.php':
require 'tester.php';
break;
default:
http_response_code(404);
exit('Not Found');
}
?>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
<a href="https://www.google.co.uk">Click here</a>
<a href="test/tester.php">Click here</a>
<a href="trythis.php">Click here</a>
<br>
<img src="images/logo.png">
</body>
</html>