0

How can I change the file permissions of a .json to allow all website visitors to write to it?

I have a PHP based website in google cloud app engine that makes a basic phone book that is editable using a .json to store the data. Once you login, You can add, delete, and change entries to the phonebook. I've done the following:

  • gotten all my files uploaded and working
  • mapped my custom domain name
  • edited the app.yaml so that PHP scripts get executed correctly
  • Gotten to display the .json
  • gotten to add an entry to the phonebook but cant get it to save the addition for the next login

Here is the important php code

$dbFile = 'phoneBook.json';
$json = file_get_contents($dbFile);
$depth = 4;
$phBook = json_decode($json, true, $depth);

// ...manipulation...

$fp = fopen($dbFile, 'w');
fwrite($fp, json_encode($phBook));
fclose($fp);

And i use that to manipulate the phonebook.json

Here is my app.yaml

runtime: php55
api_version: 1
threadsafe: true

runtime_config:
  document_root: web

handlers:

- url: /(.+\.php)$
  script: \1 

- url: /
  static_files: www/index.html
  upload: www/index.html

- url: /(.*)
  static_files: www/\1
  upload: www/(.*)

- url: /www/checklogin.php
  script: checklogin.php

The phonebook.json is in same directory as the other php files. In linux all i have to do is chmod and change the permissions to allow writes, is there anyway i can do that with what i have here? I read somewhere that you can do it if you use the compute engine VM instance instead of the app engine. is that my only choice?

4

1 回答 1

2

您不应尝试将数据存储在本地文件中。App Engine 是一种“无服务器”类型的服务 - 根据您的扩​​展设置,它将根据需要启动实例来提供流量。它还将替换实例以进行升级或它们变得不健康。

因此,您会遇到很多混乱:一些请求可能会转到不同的实例,因此在您的 phonebook.json 上显示不同的视图,因为它们将在本地加载文件。即使您将扩展限制为仅一个实例,执行升级时您也会定期丢失所有数据。

相反,使用 CloudSQL 之类的东西来存储数据。在那里,所有服务实例都将能够访问相同的数据,并且您在升级期间不会丢失任何东西。

于 2019-08-11T08:56:50.527 回答