Deploy php project with POST form on Google Cloud run

Viewed 93

I would like to deploy my php project to Google cloud run.

My goal is to have one index.php which shows some information and a Download CSV form (submit). When user click the Download CSV, there would be a POST request and goes to csv_download.php to perform the download.

index.php looks like:

// some html

<?php
echo '<form name="getcsv" method="POST" action="csv_download.php">';
echo '<input type="submit" value="Download csv" />'
echo '</form>';
?>

// some html

csv_download.php looks like:

<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="test.csv"');

$fp = fopen('php://output', 'wb');
// some code to deal with csv
fclose($fp);
?>

And the Dockerfile:

FROM php:8.0-apache

RUN docker-php-ext-install -j "$(nproc)" opcache
RUN set -ex; \
    { \
    echo "; Cloud Run enforces memory & timeouts"; \
    echo "memory_limit = -1"; \
    echo "max_execution_time = 0"; \
    echo "; File upload at Cloud Run network limit"; \
    echo "upload_max_filesize = 32M"; \
    echo "post_max_size = 32M"; \
    echo "; Configure Opcache for Containers"; \
    echo "opcache.enable = On"; \
    echo "opcache.validate_timestamps = Off"; \
    echo "; Configure Opcache Memory (Application-specific)"; \
    echo "opcache.memory_consumption = 32"; \
    } > "$PHP_INI_DIR/conf.d/cloud-run.ini"

WORKDIR /var/www/html
COPY . ./

RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

All three files are stored in the same directory.

Everything works fine when testing on localhost, when the user click the Download CSV form, test.csv will be downloaded. But when I run the project on Google gcp cloud run, csv_download.php is not working properly. The file being downloaded is not test.csv, but a file named csv_download.php with target csv content inside.

I've tried deploying and running only the csv_download.php on cloud run, it worked (the test.csv is downloaded). So I am wondering if my Dockerfile is the problem?

1 Answers

As per @MinaCheng, Neither the php code nor the Dockerfile is problematic, it is because my filename in my original code is in Chinese, which causes an encoding problem (Big5 to UTF-8).

Related