[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eThis API supports first-generation runtimes and can be used when upgrading to corresponding second-generation runtimes, with a migration guide available for PHP 7/8 runtime updates.\u003c/p\u003e\n"],["\u003cp\u003eIn the Google App Engine PHP 5 Standard Environment, the local filesystem is not writeable to ensure application security and scalability, necessitating the use of the built-in Google Cloud Storage stream wrapper for file writing.\u003c/p\u003e\n"],["\u003cp\u003eWriting to Google Cloud Storage differs from writing to a local disk, as it does not support modifying or appending to existing files, and requires creating new files to overwrite, impacting performance, especially with frequent modifications.\u003c/p\u003e\n"],["\u003cp\u003eFiles can be written to Google Cloud Storage from an app either at once or in a streamed manner, using \u003ccode\u003efile_put_contents\u003c/code\u003e or \u003ccode\u003efopen\u003c/code\u003e/\u003ccode\u003efwrite\u003c/code\u003e, with the stream wrapper using \u003ccode\u003egs://\u003c/code\u003e to identify the bucket or file object.\u003c/p\u003e\n"],["\u003cp\u003eWhile writing files at runtime in App Engine PHP 5 requires the Cloud Storage stream wrapper, reading static files uploaded with the app is possible using PHP filesystem functions like \u003ccode\u003efile_get_contents\u003c/code\u003e, as long as they are in an application subdirectory and the \u003ccode\u003eapp.yaml\u003c/code\u003e file is configured correctly.\u003c/p\u003e\n"]]],[],null,["# Reading and Writing 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\nIn the Google App Engine PHP 5 Standard Environment, the local filesystem that your application is deployed\nto is not writeable. This behavior ensures the security\nand scalability of your application.\n\nHowever, if your application needs to write and read files at runtime, the\nPHP 5 standard environment provides a built-in Google Cloud Storage stream wrapper\nthat allows you to use many of the standard\n[PHP filesystem functions](/appengine/docs/legacy/standard/php/googlestorage/advanced#filesystem_functions_support_on_cloud_storage)\nto read and write files.\n| **Important:** One major difference between writing to a local disk and writing to Google Cloud Storage is that Google Cloud Storage doesn't support modifying or appending to a file after you close it. To get an equivalent behavior in Google Cloud Storage, you must create a new file with the same name, which overwrites the original. Because of this, the *performance* characteristics of reading and writing files to Google Cloud Storage can be quite different from reading and writing to local disk, *especially when frequent modifications need to be made to the same file*.\n\nThere are two ways to write files to Google Cloud Storage:\n\n- Write files from your app\n - Simple file write\n - Streamed file write\n- Let the user upload files to Google Cloud Storage\n\nWriting files from your app\n---------------------------\n\nIf you write files from your app, you can write the entire file at once, or you\ncan stream the file write.\n| **Important:** For very large files, you may not be able to write directly from your app, because of the requirement for all app requests to complete within the [request timeout](/appengine/docs/legacy/standard/php/how-instances-are-managed#timeout). So, for very large files, you may need to [upload the file](/appengine/docs/legacy/standard/php/googlestorage/user_upload) directly, not write it from the app.\n\nThe App Engine stream wrapper for Cloud Storage is built in to the\nruntime, and is used when you supply a file name starting with `gs://`. The wrapper\nrequires the name of the bucket or file object to be in the form:\n\n`gs://bucket_name/desired_object_name`\n\n### Simple file write\n\nTo write data to Google Cloud Storage from your app, you use\n`file_put_contents`, using a valid cloud storage URL. For example: \n\n file_put_contents(\"gs://${my_bucket}/hello.txt\", $newFileContent);\n\nwhere `my_bucket` is a\n[properly configured Google Cloud Storage bucket](/appengine/docs/legacy/standard/php/googlestorage/setup).\n\nOr, if you want to use [stream options](/appengine/docs/legacy/standard/php/googlestorage/advanced#permissions_caching_and_metadata_options)\nto supply permissions, caching, and/or metadata options, you could write the\nfile as follows: \n\n $options = ['gs' =\u003e ['Content-Type' =\u003e 'text/plain']];\n $context = stream_context_create($options);\n file_put_contents(\"gs://${my_bucket}/hello_options.txt\", $newFileContent, 0, $context);\n\n### Streamed file write\n\nAlternatively, you could use `fopen`/`fwrite` to write data in a streaming\nfashion: \n\n $fp = fopen(\"gs://${my_bucket}/hello_stream.txt\", 'w');\n fwrite($fp, $newFileContent);\n fclose($fp);\n\nNote that when you use streaming, data will be flushed to Google Cloud Storage in smaller chunks.\nYou do not need to know the total length of the data to be written up front: it\nwill be calculated when the file resource is closed:\n| **Note:** You can also use [stream options](/appengine/docs/legacy/standard/php/googlestorage/advanced#permissions_caching_and_metadata_options) when using streaming file writes to supply permissions, caching, and/or metadata options.\n\nThese are the basic ways to write files. For special use cases and more advanced\nfile management, see the topics listed under [Where to go next](#where_to_go_next).\n\nDeleting files\n--------------\n\nIf you want to delete the file itself, use the PHP [unlink()](http://php.net/unlink) function.\n\nUser uploads\n------------\n\nFor details about this file write option, see\n[Allowing Users to Upload Files](/appengine/docs/legacy/standard/php/googlestorage/user_upload)\n\nReading files\n-------------\n\nFor information about reading files from Google Cloud Storage, see\n[Providing Public Access to Files](/appengine/docs/legacy/standard/php/googlestorage/public_access)\n\nSetup and Requirements\n----------------------\n\nYou need to activate Google Cloud Storage and create a bucket. See [Setup](/appengine/docs/legacy/standard/php/googlestorage/setup)\nfor more details.\n| **Note:** Some older App Engine applications may be able to use a [default bucket](/appengine/docs/legacy/standard/php/googlestorage/setup#useful_tricks_when_using_default_buckets) that has [free quota](/appengine/docs/quotas#Default_Gcs_Bucket).\n\nSupported PHP 5 filesystem functions\n------------------------------------\n\nMany of the commonly used PHP 5 file functions are supported,\nalong with file information and directory functions. For a complete list of\nsupported PHP functions, see [PHP 5 filesystem functions support](/appengine/docs/legacy/standard/php/googlestorage/advanced#filesystem_functions_support_on_cloud_storage).\n\nExtended features provided by Cloud Storage Tools API\n-----------------------------------------------------\n\nThe Google Cloud Storage stream wrapper lets you use PHP filesystem calls. However, there are\nextended features available that you may need for an optimal use of Google Cloud Storage. These\nextended features are provided in the\n[Cloud Storage Tools API](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.cloud_storage.CloudStorageTools):\n\nThis API provides a set of functions that support the serving of files and\nimages, along with other useful utilities. We'll cover several of these in\nthe other [topic pages](#where_to_go_next).\n\nIs there any other way to read and write files?\n-----------------------------------------------\n\nAn App Engine PHP 5 app must use the Cloud Storage stream wrapper\nto write files at runtime. However, if an app needs to *read* files, and these\nfiles are static, you can optionally read static files uploaded with your app\nusing PHP filesystem functions such as `file_get_contents`.\n\nFor example: \n\n $fileContents = file_get_contents($filePath);\n\nwhere the path specified must be a path relative to the script accessing them.\n\nYou must upload the file or files in an application subdirectory when you deploy\nyour app to App Engine, and must configure the `app.yaml` file so your\napp can access those files. For complete details, see\n[PHP 5 Application Configuration with `app.yaml`](/appengine/docs/legacy/standard/php/config/appref).\n\nIn the `app.yaml` configuration, notice that if you use a static file or directory handler\n(`static_files` or `static_dir`) you must specify\n`application_readable` set to true or your app won't be able to read the files.\nHowever, if the files are served by a `script` handler, this isn't necessary,\nbecause these files are readable by script handlers by default.\n\nWhere to go next\n----------------\n\nRead the following topics for details on using the Cloud Storage Tools API:\n\n- [Setup](/appengine/docs/legacy/standard/php/googlestorage/setup), quick setup instructions.\n- [Providing Public Access to Files](/appengine/docs/legacy/standard/php/googlestorage/public_access), shows how to enable users to download files via browser.\n- [Allowing Users to Upload Files](/appengine/docs/legacy/standard/php/googlestorage/user_upload), shows how to upload files via browser directly, bypassing your app.\n- [Working with Image Files](/appengine/docs/legacy/standard/php/googlestorage/images), shows optimal ways to manage and serve images.\n- [Advanced File Management](/appengine/docs/legacy/standard/php/googlestorage/advanced), covers the following:\n - Permissions, caching, and metadata stream options.\n - PHP filesystem function support.\n - Using PHP `include` and `require`.\n - Reading and writing custom metadata.\n - Cached file reads."]]