In App Engine, the local filesystem that your application is deployed to is not writeable. This behavior ensures the security and scalability of your application.
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.
There are two ways to write files to Google Cloud Storage:
- Write files from your app
- Simple file write
- Streamed file write
- Let the user upload files to Google Cloud Storage
Writing files from your app
If you write files from your app, you can write the entire file at once, or you can stream the file write.
The Google Cloud Storage stream wrapper is built in to the run time, and is used when you supply
a file name starting with
gs://. The wrapper requires the name of the bucket
or file object to be in the form:
Simple file write
To write data to Google Cloud Storage from your app, you use
file_put_contents, using a valid cloud storage URL. For example:
<?php file_put_contents('gs://my_bucket/hello.txt', 'Hello');
my_bucket is a
properly configured Google Cloud Storage bucket.
Or, if you want to use stream options to supply permissions, caching, and/or metadata options, you could write the file as follows:
$options = ['gs' => ['Content-Type' => 'text/plain']]; $ctx = stream_context_create($options); file_put_contents('gs://my_bucket/hello.txt', 'Hello', 0, $ctx);
Streamed file write
Alternatively, you could use
fwrite to write data in a streaming
<?php $fp = fopen('gs://my_bucket/some_file.txt', 'w'); fwrite($fp, 'Hello'); fclose($fp);
Note that when you use streaming, data will be flushed to Google Cloud Storage in smaller chunks. You do not need to know the total length of the data to be written up front: it will be calculated when the file resource is closed:
These are the basic ways to write files. For special use cases and more advanced file management, see the topics listed under Where to go next.
If you want to delete the file itself, use the PHP unlink() function.
For details about this file write option, see Allowing Users to Upload Files
For information about reading files from Google Cloud Storage, see Providing Public Access to Files
Setup and Requirements
If you use the default bucket available for App Engine apps, very little setup is required. See Setup for more details.
Supported PHP filesystem functions
Many of the commonly used PHP file functions are supported, along with file information and directory functions. For a complete list of supported PHP functions, see PHP filesystem functions support.
Extended features provided by Cloud Storage Tools API
The Google Cloud Storage stream wrapper lets you use PHP filesystem calls. However, there are extended features available that you may need for an optimal use of Google Cloud Storage. These extended features are provided in the Cloud Storage Tools API:
This API provides a set of functions that support the serving of files and images, along with other useful utilities. We'll cover several of these in the other topic pages.
Is there any other way to read and write files?
An App Engine PHP app must use the Cloud Storage stream wrapper to write
files at runtime. However, if an app needs to read files, and these files are
static, you can optionally read static files uploaded with your app using
PHP filesystem functions such as
where the path specified must be a path relative to the script accessing them.
You must upload the file or files in an application subdirectory when you deploy
your app to App Engine, and must configure the
app.yaml file so your
app can access those files. For complete details, see
PHP Application Configuration with
app.yaml configuration, notice that if you use a static file or directory handler
static_dir) you must specify
application_readable set to true or your app won't be able to read the files.
However, if the files are served by a
script handler, this isn't necessary,
because these files are readable by script handlers by default.
Where to go next
Read the following topics for details on using the Cloud Storage Tools API:
- Setup, quick setup instructions.
- Providing Public Access to Files, shows how to enable users to download files via browser.
- Allowing Users to Upload Files, shows how to upload files via browser directly, bypassing your app.
- Working with Image Files, shows optimal ways to manage and serve images.
- Advanced File Management, covers the following:
- Permissions, caching, and metadata stream options.
- PHP filesystem function support.
- Using PHP
- Reading and writing custom metadata.
- Cached file reads.