Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Reading and Writing Files

Python | Java | PHP | Go

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 (GCS) 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 GCS:

  • Write files from your app
    • Simple file write
    • Streamed file write
  • Let the user upload files to GCS

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

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:

<?php
file_put_contents('gs://my_bucket/hello.txt', 'Hello');

where my_bucket is a properly configured GCS 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 fopen / fwrite to write data in a streaming fashion:

<?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 GCS 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 GCS, see Providing Public Access to Files

Setup and Requirements

If you use the default GCS 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 GCS stream wrapper lets you use PHP filesystem calls. However, there are extended features available that you may need for an optimal use of GCS. 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 file_get_contents .

For example:

<?php
file_get_contents('config/my_configuration.json');

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 .

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:

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.