使用文本文件存储数据

I want to to read and write some data to text files from my PHP app. Its a small amount of data. Really just configurations.

My concern is that I have no control over the deployment environment. It will be a mixed bag of servers, mac and windows. They will all be running PHP5.3 and greater. The mac servers will have apache, and the windows servers will be running IIS.

I don't want customers calling with issues related to server setup and or permissions on files/folders.

I am pretty sure that WordPress does this all the time so, I know this is possible to do cleanly. The questions is how? Does anyone have any suggestions, pointers to libraries, or strategies that will help me accomplish my goal.

I ruled out sqlLite for this purpose, because i don't think it is enabled by default on windows and I think it is no longer installed on php5.4 windows by default. My main goal is to be able to persist a small amount of data in such a way that does drive my support costs through the rough the roof.

Yeah, but even with Wordpress you have to worry about making certain files and directories writable (chmod, windows file permissions, etc).

And if I'm not mistaking Joomla/Wordpress (one of them at least) also give you the opportunity to enter FTP credentials, so rather than editing the file through the filesystem, it will try to upload the edited version through the FTP server.

What you simply could do is have a config file that must be edited in a text editor. And have the text-file read only for the webserver / application. But then changing one of these setting cannot be done through the website itsself, but needs a person to edit the text file in a text editor.

If you are already using a database, then ONLY the database-settings would have to be in the config file. The rest can be stored in the database.

The way all common big PHP projects do it afaik is simply by reserving a folder for it, and checking its permissions on install.

You could easily make a /gen or /data folder in your webroot, and on install/update check that it:

  1. contains a .htaccess file stating deny from all if the webserver is Apache, or an equivalent method of protection (just file_get_contents via the public URL to test) on other webservers
  2. is_writable (you could also write, read and delete a small sample file to ensure this)

Put your documents in there and it's safe and portable on every platform.

Some sample code:

$docroot = $_SERVER['DOCUMENT_ROOT'];
$dataroot = $docroot.'/data';
$testfile = $dataroot.'/test.txt';
$publicURL = $youHaveThisSomewhere.'/data/test.txt';
if(!is_dir($dataroot))
  die("The required data folder is not present at $dataroot");
if(!is_writable($dataroot) || file_put_contents($testfile, 'test') === FALSE)
  die("Data path ($dataroot) is not writable, make it so!");
if(file_get_contents($publicURL) !== FALSE)
  die("Data path is publicly accessible, go fix it!");
if(!unlink($testfile))
  die("I also need delete rights in the data folder!");

die("Installation successful!");

I think the option you may be looking for involves the following steps:

Create an install file in this install script file:

  1. See if directory is writable
  2. If not ask for FTP credentials
  3. Determine the system type Windows or *Nix bassed
  4. Establish an FTP connection
  5. *nix variants send a CHMOD command Windows send a CACLS command to make the directory writable
  6. Terminate the FTP command