Search code examples
htmlgoogle-app-enginegoogle-cloud-platformanchorphp-7.2

Anchor and Img tags not finding PHP files or images


I guess normally what seems to work for everyone else (and what I want to do) is:

<img src="/images/logo.png"> or <img src="images/logo.png">

Both of these options work locally (on the GCP App Engine).

When I use the command gcloud app deploy in the shell, my understanding is that it deploys the website. The code no longer works, the images are not found, and there are no errors reported (just little blue boxes where the images should be).

I have tried numerous variations, tried moving files into different folders, and read these two pages:

Differences in declaring your root directory in HTML

Pick images of root folder from sub-folder

My file structure can be described as:

HelloWorld
    images
        logo.png
    test
        tester.php
    trythis.php
    index.php

Where HelloWorld, test and images are folders.

I actually use the GCP Storage to host and store my images, the issue I am having is with hyperlinks, the anchor tag, and having a link from index.php to test.php. The link cannot find the file, so it is the same issue.

EDIT: complete code

<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>

And my app.yaml:

runtime: php72

All works locally, none of it works on server.


Solution

  • You need to setup your app.yaml that include your directory folder and image type, so App Engine serves the files. Try to deploy with this 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)$
    

    A line that begins with a pound (#) character is ignored or this is a comment. To know more about app.yaml, visit this link.

    For PHP files, to handle all request routing you will need to add a front controller. You can specify it in index.php link the example below or create a new file for front end controller but make sure that you will add this line entrypoint: serve path/to/my/front/controller.php in 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>