PHP Flat-File Content Management

Build fast, lightweight content sites with PHP and flat files for effortless page management and instant deployment.

Ceyhun Enki Aksan
Ceyhun Enki Aksan Entrepreneur, Maker

I’ve been working on WordPress improvements and critical issues for a long time, and I’ve been focusing very little on PHP lately. However, recently, a friend of mine opened up a discussion about his portfolio page, and I’d like to briefly touch upon PHP in the context of file-based content production.

Of course, there are many programming languages that could be used for this particular example. In this article, I’ll provide a basic example, and then discuss more advanced tools and applications available.

PHP Flat-File Data Management

Flat-File, or file-based operations, execute without any external tools directly through PHP’s file system functions (fopen, fwrite, fclose, etc.)1. The intention here may be to use a database server, but you can still manage a SQL file without a MySQL server. Below, I’m sharing an example that demonstrates recording and editing operations on a text file23.

<?php
$currentFile = "content.txt";
if (isset($_POST['content'])) {
  $newData = nl2br(htmlspecialchars($_POST['content']));
  $currentData = fopen($currentFile, "w");
  fwrite($currentData, $newData);
  echo (!file_exists($currentFile)) ?: $getContent = file_get_contents($currentFile);
  echo str_replace("<br />", "", $getContent) . ' ';
  fclose($currentData);
}
?>

<form action="./" method="POST">
  <textarea name="content"></textarea>
  <input type="submit" value="Submit" />
</form>

Example code initially creates a content.txt file and writes the input from the textarea field. Subsequently, every change made in the textarea field is also reflected in the content of this file. This is the most fundamental way to illustrate the flat-file structure. For instance, the operation of using Nazım Hikmet Ran’s poem “To Your Hands and to the Blank” as the content of content.txt is shown below.

Php flat file
Php flat file

Project FFDB-PHP

Of course, beyond simple usage, this structure can be employed in various advanced and purpose-specific ways. As an example, the first one I’ll present is published under the name Project FFDB-PHP4. Project FFDB-PHP offers numerous features and supports a wide range of data types. For detailed information and class usage, please refer to the official website5 6 at7. Example implementations are available within the folder contents.

FlatSQL

FlatSQL is one of the successful and efficient examples that performs file operations via SQL. However, it has not been actively developed for a long time. You can access its code and usage examples through its GitHub page8.

Kirby

Kirby is one of the implementations that applies file-based data management at the level of content management systems (CMS). You can find detailed information about the application that enables content creation using Markdown rules within folders at [getkirby.com]9.

Grav

There are numerous previously published articles related to Grav. For answers to questions such as “What is Grav and how do I install it?”, please refer to my article Grav: What is it and How to Install It.

HTMLy is also developed using PHP, one of the successful examples being Kirby and Grav. For more details about HTMLy, you can explore its web page10 11.

Web Page Management via Dropbox

Yes, you can manage your web pages by storing your files in cloud file hosting and sharing applications such as Dropbox, Google Drive. You do not need a shared database server. For this purpose, the article “7 Dropbox-Powered Site Creation Platforms” has been published, featuring various applications with different features12.

*[SQL]: Structured Query Language
*[PHP]: PHP: Hypertext Preprocessor
*[CMS]: Content Management System

Footnotes

  1. File System. PHP Documentation
  2. Luke Plant. Flatfile Tutorial
  3. PHP Flat-file Beginner Log-in Script. Code Project
  4. Flat File DataBase (FFDB) for PHP
  5. Flat File DataBase Class
  6. PHP FFDB Examples. Hot Examples
  7. ffdb.inc.php. Flat File DataBase Class
  8. dhtml/flatsql. GitHub
  9. Matthew Guay. (2013). Kirby: The Flat-File CMS That’ll Make You Rethink Content Management. AppStorm
  10. Htmly Demo
  11. Htmly
  12. Kezz Bracey. (2014). 7 Dropbox-Powered Site Creation Platforms. envato tuts+