PHP 5 has reached end of support and will be
deprecated
on January 31, 2026. After deprecation, you won't be able to deploy PHP 5
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing PHP
5 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you
migrate to the latest supported version of PHP.
Providing Public Access to Files
Stay organized with collections
Save and categorize content based on your preferences.
A common use case is making your files publicly accessible via the web.
You can do this in the PHP 5 standard environment in any of these ways:
- Serve files in Google Cloud Storage from a script: your app serves the file.
- Serve files from Google Cloud Storage, which serves the file directly.
- Serving files uploaded with your app by using the static handler in
app.yaml
.
Note that the last methodology does not use Cloud Storage.
Serving files from a script
If you want to serve files from your app, import the App Engine
CloudStorageTools
class:
Now use CloudStorageTools::serve
to serve the file from Google Cloud Storage:
Serving files from the app in this way allows the developer to determine user
identity and ensure that only authorised users access the file. The downside
to this approach is that your application needs to run this code to serve the
file, which consumes instance hours and thus incurs cost.
Serving files directly from Google Cloud Storage
There is a faster and more cost-effective way to serve files than serving them
from the app, as mentioned above: serving them from Cloud Storage directly via
HTTP. The files need to be configured to be readable by anonymous users at file
write time. As we'll show in the snippet below, you set the acl
stream option
to public-read
.
Once the file is written to Cloud Storage as publicly readable, you need to
get the public URL for the file, using
CloudStorageTools::getPublicUrl.
In the following example, we create a public-readable file containing some
random numbers, writing it to a Cloud Storage bucket, and redirect to
that file from Cloud Storage.
The limitation of this approach is that there is no control over who can access
the file, because the file is readable by anyone.
Serving files uploaded with your app
This option is fully described under
Is there any other way to read and write files.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-29 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-29 UTC."],[[["\u003cp\u003eThis API supports first-generation runtimes and is applicable when upgrading to corresponding second-generation runtimes, while migration to App Engine PHP 7/8 runtime requires consulting the migration guide.\u003c/p\u003e\n"],["\u003cp\u003eFiles can be made publicly accessible via the web in the PHP 5 standard environment by serving them from a script, serving them directly from Google Cloud Storage, or using the static handler in \u003ccode\u003eapp.yaml\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eServing files from a script through the App Engine \u003ccode\u003eCloudStorageTools\u003c/code\u003e class allows for user identity verification, but it consumes instance hours, and serving directly from Cloud Storage is faster and more cost-effective, but it lacks user access control.\u003c/p\u003e\n"],["\u003cp\u003eTo serve files directly from Cloud Storage, files must be configured as readable by anonymous users, utilizing the \u003ccode\u003epublic-read\u003c/code\u003e ACL setting and \u003ccode\u003eCloudStorageTools::getPublicUrl\u003c/code\u003e to obtain the public URL.\u003c/p\u003e\n"],["\u003cp\u003eServing files uploaded with your app can be achieved, for more details consult the link provided regarding reading and writing files.\u003c/p\u003e\n"]]],[],null,["# Providing Public Access to Files\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| php-gen2\n|\n| /services/access). If you are updating to the App Engine PHP 7/8 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/php-differences) to learn about your migration options for legacy bundled services.\n\n\u003cbr /\u003e\n\nA common use case is making your files publicly accessible via the web.\nYou can do this in the PHP 5 standard environment in any of these ways:\n\n- Serve files in Google Cloud Storage from a script: your app serves the file.\n- Serve files from Google Cloud Storage, which serves the file directly.\n- Serving files uploaded with your app by using the static handler in `app.yaml`.\n\nNote that the last methodology does not use Cloud Storage.\n\nServing files from a script\n---------------------------\n\nIf you want to serve files from your app, import the App Engine\n`CloudStorageTools` class: \n\n use google\\appengine\\api\\cloud_storage\\CloudStorageTools;\n\nNow use [CloudStorageTools::serve](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.cloud_storage.CloudStorageTools#method_serve)\nto serve the file from Google Cloud Storage: \n\n CloudStorageTools::serve(\"gs://${my_bucket}/serve.txt\");\n\nServing files from the app in this way allows the developer to determine user\nidentity and ensure that only authorised users access the file. The downside\nto this approach is that your application needs to run this code to serve the\nfile, which consumes instance hours and thus incurs cost.\n\nServing files directly from Google Cloud Storage\n------------------------------------------------\n\nThere is a faster and more cost-effective way to serve files than serving them\nfrom the app, as mentioned above: serving them from Cloud Storage directly via\nHTTP. The files need to be configured to be readable by anonymous users at file\nwrite time. As we'll show in the snippet below, you set the `acl` stream option\nto `public-read`.\n\nOnce the file is written to Cloud Storage as publicly readable, you need to\nget the public URL for the file, using\n[CloudStorageTools::getPublicUrl](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.cloud_storage.CloudStorageTools#method_getPublicUrl).\n\nIn the following example, we create a public-readable file containing some\nrandom numbers, writing it to a Cloud Storage bucket, and redirect to\nthat file from Cloud Storage. \n\n $options = ['gs' =\u003e ['acl' =\u003e 'public-read']];\n $context = stream_context_create($options);\n $fileName = \"gs://${my_bucket}/public_file.txt\";\n file_put_contents($fileName, $publicFileText, 0, $context);\n\n $publicUrl = CloudStorageTools::getPublicUrl($fileName, false);\n\nThe limitation of this approach is that there is no control over who can access\nthe file, because the file is readable by anyone.\n\nServing files uploaded with your app\n------------------------------------\n\nThis option is fully described under\n[Is there any other way to read and write files](/appengine/docs/legacy/standard/php/googlestorage#is_there_any_other_way_to_read_and_write_files)."]]