PHP Flexible Environment: Write access to directory possible?

1,120 views
Skip to first unread message

Jörg Hartgen

unread,
Nov 1, 2017, 5:42:09 PM11/1/17
to Google App Engine
Dear all,

is it possible to make a directory writeable for the app?

In PHP App Engine / Standard Environment it's documented as not possible:
Therein:
"In App Engine, the local filesystem that your application is deployed to is not writeable. [...]
However, if the application needs to write and read files at runtime, App Engine provides a built-in Google Cloud Storage stream wrapper that allows you to use many of the standard PHP filesystem functions to read and write files in an App Engine PHP app."

But what about App Engine / Flexible Environment? I cannot find a documentation if it is possible to configure a directory as writeable.

If it's not possible, what can I do to make it possible in the google cloud environment?

It's a standard app, so it's not possible for me to make changes in the app itself.

Best regards
Jorg

George (Cloud Platform Support)

unread,
Nov 2, 2017, 11:06:30 AM11/2/17
to Google App Engine
Hello Jörg, 

The flexible environment does allow you to write to a directory on a local disk, but written files are ephemeral, due to the fact that the disk gets initialized on each VM startup, so your files will get deleted on this occasion. You can read more related detail on the "Choosing an App Engine Environment" online documentation page

You can profit from the facilities offered by the Compute Engine, if writing to disc is a must. By default, each Compute Engine instance has a single root persistent disk that contains the operating system, as stated on the "Storage Options" page. For more insight, you may implement the proposed example from the "Running the PHP Bookshelf on Compute Engine" page. 

The Compute Engine does not handle scaling up of your applications automatically, so you'll have to take care of it yourself, as suggested in the "Autoscaling Groups of Instances" document

Jörg Hartgen

unread,
Nov 2, 2017, 1:12:40 PM11/2/17
to Google App Engine
Hello George,

thanks a lot for making this clear. Before I choosed the flexible environment I was not aware, that file system operations are restricted.

I'll try to set it up in a compute engine environment.

Jörg

George (Cloud Platform Support)

unread,
Nov 2, 2017, 3:26:10 PM11/2/17
to Google App Engine
Hi Jörg, 

Glad to hear that the information has helped you take a decision that suits your purposes. 

Why is it absolutely necessary to write to the local disc? If you simply need to store data in a persistent manner, you may consider using cloud storage, as detailed in the "Using Cloud Storage" document, specifically for flex and PHP. This option would allow you to profit from the automatic scaling and other advantages of a managed environment, while still having access to permanently stored data, as in a file system. 

Jörg Hartgen

unread,
Nov 2, 2017, 3:41:18 PM11/2/17
to Google App Engine
Hi George,

yes, I'd like to use the flexible environment because of it's scaling and other advantages.

I'm using a PHP/MySQL rapid development system that needs write access to a specified directory. It's a requirement.

So it would be possible to use the flexible environment if there is a way to "map" one specified directory name (in my case "template_c") to a cloud storage bucket.

Is there a way to do so?

Jörg Hartgen

unread,
Nov 2, 2017, 3:49:06 PM11/2/17
to Google App Engine
In addition to my last post: The development system uses "Smarty", a template engine that needs write access to a specified directory during runtime. Details:

Requirement:

Applications use the Smarty library. Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. Smarty compiles copies of the templates as PHP scripts. Compilation happens once when each template is first invoked, and then the compiled versions are used from that point forward. The compiled templates are stored under the templates_c directory which must be accessible for writing by Smarty. To upload a ready web application to your web hosting, you need to have the following conditions. 1. Allow write access to templates_c The web server user must have write access to the templates_c directory. The most secure method is to make this directory owned by this user. The change of ownership is the easiest way to accomplish this. Only an administrator can execute this operation so if you can't do that then ask your hosting provider to do it for you. If you can do this, then it is possible (and recommended) to disallow "other" users read/write access for optimum security. 

George (Cloud Platform Support)

unread,
Nov 2, 2017, 4:17:35 PM11/2/17
to Google App Engine
However, if the application needs to write and read files at run-time, App Engine provides a built-in Google Cloud Storage stream wrapper that allows you to use many of the standard PHP filesystem functions to read and write files in an App Engine PHP app. This might prove a fitting solution for your requirements. You may check details in the PHP-related document "Reading and Writing Files". 

Jörg Hartgen

unread,
Nov 2, 2017, 4:28:23 PM11/2/17
to Google App Engine
The document "Reading and Writing Files" is related to the "Standard Environment". Is it also valid for the "Flexible Environment"?

George (Cloud Platform Support)

unread,
Nov 2, 2017, 4:41:35 PM11/2/17
to Google App Engine
There is a PHP example for the flexible environment in the "Using Cloud Storage" document for PHP and flex. You may consider checking it for applicable sample code, to adapt to your particular requirements. 

Takashi Matsuo

unread,
Nov 16, 2017, 2:23:34 PM11/16/17
to google-a...@googlegroups.com
Hi Jörg,

The Flex build pipeline set a stcict permission on everything under document_root.
Is it possible to configure smarty to use a directory outside of the document_root (e.g. /tmp, or /app/smarty_cache)?

Technically it's possible to introduce a flag for not setting the strict permission at all, so let us know if you want this option.

On Thu, Nov 2, 2017 at 1:42 PM 'George (Cloud Platform Support)' via Google App Engine <google-a...@googlegroups.com> wrote:
There is a PHP example for the flexible environment in the "Using Cloud Storage" document for PHP and flex. You may consider checking it for applicable sample code, to adapt to your particular requirements. 

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/e8db7b1d-54c1-422f-8772-0ac0728a3776%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
-- Takashi

Jörg Hartgen

unread,
Nov 17, 2017, 4:00:43 PM11/17/17
to Google App Engine
Dear Takashi,

yes, this could be a solution.

We investigated the code and found that the templates_c directory is hardcoded in smarty in just two places:

components/renderers/template_renderer.php
and
components/utils/check_utils.php

We could correct both these places and add the files above to the list of excluded files to prevent them from being overwritten when updating the code.

In template_renderer.php:

###########
<?php

include_once dirname(__FILE__) . '/' . '../../libs/smartylibs/Smarty.class.php';

interface ITemplateRenderer
{
public function render($templateName, $params);
}

class SmartyTemplateRenderer implements ITemplateRenderer
{
/** @var Smarty */
private $smarty;

/**
* @param string $templateDirectory
* @param string $compileDirectory
*/
public function __construct($templateDirectory = 'components/templates', $compileDirectory = 'templates_c') {
$this->smarty = new Smarty();
$this->smarty->template_dir = $templateDirectory;
$this->smarty->compile_dir = $compileDirectory;
}

/**
* @param string $templateName
* @param array $params
* @return string
*/
public function render($templateName, $params) {
foreach($params as $key => &$value) {
if (is_object($value)) {
$this->smarty->assign_by_ref($key, $value);
} else {
$this->smarty->assign($key, $value);
}
}

return $this->smarty->fetch($templateName);
}

}

function GetTemplateRenderer() {
return new SmartyTemplateRenderer();
}


###########

In check_utils.php there is only a test if the templates_c directory is writeable:

###########
function CheckTemplatesCacheFolderIsExistsAndWritable() {
$templatesCacheFolder = 'templates_c';
if (!file_exists($templatesCacheFolder) || !is_writable($templatesCacheFolder)) {

header('Content-Type: text/html; charset=UTF-8');

$result = file_get_contents('components/templates/templates_c_folder_warning.html');
$result = str_replace('{MESSAGE}', 'Error: the templates_c directory does not exist or is not writable', $result);
$result = str_replace('{DETAILS}', 'Please make sure that the templates_c directory does exist in the root directory of the generated application and it is writable by the web server user.', $result);
echo $result;
exit;
}
}

How can we proceed?

Cheers
Jorg

Takashi Matsuo

unread,
Nov 20, 2017, 5:56:25 PM11/20/17
to google-a...@googlegroups.com

Hi Jörg,

It seems like there is a method call to change the compile dir (and also cache dir).

See:


For more options, visit https://groups.google.com/d/optout.
--
-- Takashi
Reply all
Reply to author
Forward
0 new messages