Reading and Writing Files

In the Google App Engine PHP 5 Standard Environment, the local filesystem that your application is deployed to is not writeable. This behavior ensures the security and scalability of your application.

However, if your application needs to write and read files at runtime, the PHP 5 standard environment 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.

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 App Engine stream wrapper for Cloud Storage is built in to the runtime, 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:

gs://bucket_name/desired_object_name

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:

file_put_contents("gs://${my_bucket}/hello.txt", $newFileContent);

where 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']];
$context = stream_context_create($options);
file_put_contents("gs://${my_bucket}/hello_options.txt", $newFileContent, 0, $context);

Streamed file write

Alternatively, you could use fopen/fwrite to write data in a streaming fashion:

$fp = fopen("gs://${my_bucket}/hello_stream.txt", 'w');
fwrite($fp, $newFileContent);
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.

Deleting files

If you want to delete the file itself, use the PHP unlink() function.

User uploads

For details about this file write option, see Allowing Users to Upload Files

Reading files

For information about reading files from Google Cloud Storage, see Providing Public Access to Files

Setup and Requirements

You need to activate Google Cloud Storage and create a bucket. See Setup for more details.

Supported PHP 5 filesystem functions

Many of the commonly used PHP 5 file functions are supported, along with file information and directory functions. For a complete list of supported PHP functions, see PHP 5 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 5 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 file_get_contents.

For example:

$fileContents = file_get_contents($filePath);

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 5 Application Configuration with app.yaml.

In the app.yaml configuration, notice that if you use a static file or directory handler (static_files or 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: