[[["わかりやすい","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 UTC。"],[[["\u003cp\u003eThis API supports first-generation runtimes and is used when upgrading to second-generation runtimes, with specific migration guidance available for App Engine PHP 7/8 runtimes.\u003c/p\u003e\n"],["\u003cp\u003eThe App Engine stream wrapper for Cloud Storage allows you to configure stream options like \u003ccode\u003eacl\u003c/code\u003e, \u003ccode\u003eContent-Type\u003c/code\u003e, \u003ccode\u003eContent-Disposition\u003c/code\u003e, and \u003ccode\u003eContent-Encoding\u003c/code\u003e, along with caching directives like \u003ccode\u003eenable_cache\u003c/code\u003e and \u003ccode\u003eenable_optimistic_cache\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eMany PHP filesystem functions are supported with the App Engine stream wrapper for Cloud Storage, but some, including \u003ccode\u003echgrp\u003c/code\u003e, \u003ccode\u003echmod\u003c/code\u003e, and \u003ccode\u003echown\u003c/code\u003e, are not supported and will always return \u003ccode\u003efalse\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eCustom metadata can be added to files being written to Google Cloud Storage by including a \u003ccode\u003emetadata\u003c/code\u003e array in the \u003ccode\u003e$options\u003c/code\u003e when creating the stream context, and this metadata can be retrieved through \u003ccode\u003eCloudStorageTools\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eFile reads are cached in memcache by default to improve performance, and this behavior can be modified with caching directives, including disabling it completely or implementing optimistic caching for write-once, read-many situations.\u003c/p\u003e\n"]]],[],null,["# Advanced PHP 5 File Management\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\nPermissions, caching and metadata options\n-----------------------------------------\n\nThe App Engine stream wrapper for Cloud Storage provides the following\noptions to configure your stream:\n\nThe following snippet shows how to use stream options: \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\nIn the snippet above, `$options` is a set of arguments that the stream will use\nwhen writing new objects, which can be set as the default options using\n[`stream_context_set_default`](http://php.net/manual/function.stream-context-set-default.php).\n\nPHP 5 filesystem functions support on Cloud Storage\n---------------------------------------------------\n\nThe App Engine stream wrapper for Cloud Storage supports many native PHP\nfilesystem functions. Some functions are not supported, and some have modified\nsupport. The following table lists each of these native functions, indicating\nwhether the function is supported or not. If a function is supported but with\nmodifications or limitations, those are described.\n\nNote that the file stat functions in the table above (`file_exists`, `filemtime`,\n`filesize`, `fstat`, `is_file`, `is_dir`, `is_writable`, and `stat`) are cached\non a per-request basis. You can clear this cache by calling `clearstatcache`.\n\nIn addition, the following PHP directory-reading functions are supported:\n\n| **Note:** As Cloud Storage listing operations are [eventually consistent](/storage/docs/consistency), `readdir` might not reflect the change immediately after an object is added, renamed or removed.\n\nUsing PHP 5 `include` and `require`\n-----------------------------------\n\nTo help maintain the security of your application, the ability to `include` or\n`require` from a Cloud Storage file is disabled by default, but you can enable\nit, as follows:\n\nTo [`include`](http://php.net/manual/function.include.php) or\n[`require`](http://php.net/manual/function.require.php) PHP code\nfrom Google Cloud Storage in your application, you must specify which buckets\ncontain those files using the `google_app_engine.allow_include_gs_buckets`\ndirective in your [`php.ini`](/appengine/docs/legacy/standard/php/config/php_ini) file.\n\nReading and writing custom metadata\n-----------------------------------\n\nTo attach custom metadata to a file being written to Google Cloud Storage, add\nthe metadata to `$options` before creating the stream context used for\nthe [file_put_contents](http://php.net/file-put-contents.php) call.\n\nIn this example, metadata named `foo` is given the value `far`,\nand another named `bar` is given the value `boo`. The example also sets the\ncontent type to `text/plain`. The stream context is created using this\ninformation in `$options` as shown, and the file is written to Cloud Storage by\nusing `file_put_contents()`, along with the metadata and content type: \n\n $metadata = ['foo' =\u003e 'bar', 'baz' =\u003e 'qux'];\n $options = [\n 'Content-Type' =\u003e 'text/plain',\n 'metadata' =\u003e $metadata\n ];\n $context = stream_context_create(['gs' =\u003e $options]);\n file_put_contents(\"gs://${my_bucket}/hello_metadata.txt\", $newFileContent, 0, $context);\n\nTo read the custom metadata and content type for a file, call `fopen` on the\nfile and then use [`CloudStorageTools::getContentType()`](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.cloud_storage.CloudStorageTools#method_getContentType)\nto get the content type and [`CloudStorageTools::getMetaData()`](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.cloud_storage.CloudStorageTools#method_getMetaData)\nto get the metadata.\n\nThe custom metadata and content type are available once you have the file\npointer returned from the `fopen` call. \n\n $fp = fopen(\"gs://${my_bucket}/hello_metadata.txt\", 'r');\n $content_type = CloudStorageTools::getContentType($fp);\n $metadata = CloudStorageTools::getMetaData($fp);\n\nCached file reads\n-----------------\n\nBy default, the App Engine stream wrapper for Cloud Storage caches file\nreads into [memcache](/appengine/docs/legacy/standard/php/memcache) to improve\nperformance on subsequent reads. Caching can be turned off using the `enable_cache`\ndirective in the stream context.\n| **Note:** When caching is enabled, files read from Cloud Storage are cached as they are read, and the cache also contains the file information regarding writability. So, be aware that `is_writable` could be querying stale file information. Also, if you change the ACL on a Cloud Storage bucket to either grant or deny write permissions to the application, this change might take some time to be reflected.\n\nFor further performance improvement, you can also enable optimistic caching, which\nwill read the file object from the cache without seeing whether the underlying object\nwas changed in Google Cloud Storage since the last time it was cached, using\nthe `enable_optimistic_cache` directive (set to `false` by default).\nOptimistic caching is ideal for write-once, read-many scenarios.\n\nYou can also change the length of time\nthat a cached object remains valid by using the `read_cache_expiry_seconds` directive,\nafter which time the object will be re-cached upon the next read attempt. By default\nthis is set to 1 hour (`3600`).\n\nIn this example, optimistic caching is turned on, and file objects are configured\nto be marked for re-caching from Google Cloud Storage after 5 minutes. \n\n $options = [\n 'gs' =\u003e [\n 'enable_cache' =\u003e true,\n 'enable_optimistic_cache' =\u003e true,\n 'read_cache_expiry_seconds' =\u003e 300,\n ]\n ];\n $context = stream_context_create($options);\n file_put_contents(\"gs://${my_bucket}/hello_caching.txt\", $newFileContent, 0, $context);"]]