0

I'm going to be using a JSON file to contain a list of links to a few posts, and it can be updated at any time. However, I'm stumped with the mode to use with PHP's fopen() function. This will be a flat-file database and primarily is for me learning to work with files, PHP, and JSON before moving onto a proper relational database (that, and it's not a huge collection of pages that I'm worried about using SQL or something like that yet...)

The process I'm using is that once a blog post is typed up, it will create a directory, save a new index.php file to it with all of the stuff that lets me view the page, and then, where I'm currently stuck, update a JSON file with the Title, Author, Date, and link to the newly created page.

Based on the PHP Manual, there are three modes I might want to use. r+, w+, or a+.

The process I am looking to use is to take the JSON file and place the data into an array. Update the array, then save it back to the file.

a+ places the pointer at the end of the file and writes are always appended, so I'm assuming this is the worst choice for this situation since I wouldn't add a new JSON entry at the end of the file (I'm tempted to actually insert any new data at the beginning of the JSON object instead of at the end).

w+ mentions read and write, but also truncating the file - does this happen upon saving data to the file, or does this happen the moment the file is opened? If I used this mode on an existing JSON file, would I then be reading a blank file before I can even modify the array and re-save it to the object?

r+ mentions placing the pointer at the beginning of the file - does saving data overwrite what's there or will it insert the data BEFORE what's existing there? If it inserts, how would I manually clear the file and then save the newly-modified array to the JSON object?

Which of those modes are best suited for what I'm looking to do? Is there a better way of doing this, anyway?

5
  • 2
    I'd use file_get_contents to read the file and the w+ option to save it. Commented Oct 18, 2021 at 8:19
  • "Best suited" depends on what you want to do. Do you want to modify the JSON structure by hand, or using json_encode? Commented Oct 18, 2021 at 8:33
  • @NicoHaase If by hand, you mean I'd go in via something like notepad or VS Code and manually add entries, no, I'd be doing it in code probably with json_encode Commented Oct 19, 2021 at 1:17
  • If you plan doing this using json_encode, how would you modify only parts of that array? Commented Oct 19, 2021 at 5:15
  • Given what I've found so far, it seems the most feasible option is to read the JSON file, copy it to a variable, modify that, then re-write it to the file. Unless there's a better way I don't know of lol Commented Oct 22, 2021 at 2:19

2 Answers 2

4

If you're always reading or writing an entire file, you don't have to work with file handles at all - PHP provides a pair of functions file_get_contents($file_name) and file_put_contents($file_name, $content) which are much simpler to work with.

File handles with their various modes are most useful when you're working with parts of files. For instance, if you are using CSV, you can read or write one line at a time, without having the full set of data in memory at once. Or, with binary file formats, you might know the location in the file you want to read from, and can "seek" the file handle to that location.

Sign up to request clarification or add additional context in comments.

Comments

1

You should probably read the entire file first (eg with file_get_contents(), and then open it with w+ to write the new data. (Edit: Or rather, as the other answer points out, use file_put_contents(), which is always simpler when you are only making one write operation.)

r+ will overwrite as much of the file as you are writing, but won't erase beyond that. If your data always increases in size, this should be the same as overwriting the file entirely, but even if it's true now, that's an assumption that will likely mess up your data in the future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.