DavidAlanBruce DavidAlanBruce - 4 months ago 16
YAML Question

Retrieve .yaml data, using PHP

I'm using a flat-file CMS to save simple, user submitted form posts to a folder.

They contain the username of the user that submitted the post. And the text that they submitted in the form:

folders & files

(I've saved these submissions as .yaml, but another text format might be more efficient.)




My Aim, is to create a dashboard for the current user, that parses the text submissions, and displays them on the page. Resulting in something like this.

Desired Page Output

I want to retrieve the text data only if the current users username matches the username in the folder.




I'd appreciate any help, from if i'm thinking about the process in an effeciant way, to how I'd pull the .yaml data to the page.

Thank you.

Answer

I would use JSON for this instead of yaml, since there already is support for JSON in PHP. Yaml makes more sense if you have configuration files that needs to be really readable and that users should edit manually.

Write example:

file_put_contents('/path/to/file', json_encode($_POST));

Read example:

$content = json_decode(file_get_contents('/path/to/file'), true);

The $content variable will now have the same content as your $_POST variable did.

The second argument, true, in json_decode() makes sure that we get an associative array instead of an object.

Read more about json_encode() and json_decode().