Jump to content

Updating an array held in a text file


Scaryminds
Go to solution Solved by requinix,

Recommended Posts

This one has me stumped, have tries json and file_put_contents() to no avail.

 

Okay have a text file, see contents below:

<?php

    site['subTitle'] = "Welcome to the Site";
?>

 On an administration page I want to change "Welcome to the Site" to "This is Sydney" or some such, and then write out the array so I get

<?php

    site['subTitle'] = "This is Sydney";
?>

Can't work out how to write the array to the text file in the format above,

 

Thanks in advance team.

Link to comment
Share on other sites

  • Solution

JSON would be really nice.

 

{
	"subTitle": "Welcome to the Site"
}
// read
$site = json_decode(file_get_contents("/path/to/file.json"), true);

// write
file_put_contents("/path/to/file.json", json_encode($site));

You do lose some of the PHP caching, though. To write out the array to a PHP file, use var_export.

Link to comment
Share on other sites

First off: Dynamically generating PHP code and loading that into your application is the absolute last thing you want. If anybody manages to manipulate that file (which is fairly likely), you end up executing random code on your server. Or if there's just a tiny syntax error in the file, your entire application is dead.

 

Do not generate PHP code.

 

The best place for configuration values is the database. Using flat files is generally not a good idea, because this quickly results in data corruption:

  • File operations like file_put_contents() can actually fail. If you write directly to your configuration, you'll end up with garbage and kill your application.
  • There can be multiple file operations at the same time. Without concurrency control (e. g. locking), this may also put your configuration into an inconsistent state or make you lose changes.

Long story short: Use the database.

Edited by Jacques1
Link to comment
Share on other sites

I'm so going to regret this.

 

Dynamically generating PHP code and loading that into your application is the absolute last thing you want.

I agree in principle, but OP isn't generating code. It's about storing data in a file. It just happens that data is being represented with a PHP array.

 

If anybody manages to manipulate that file (which is fairly likely), you end up executing random code on your server.

Putting aside the stupid "fairly likely" exaggeration, if someone has the ability to edit files then they wouldn't stop at this one file.

 

Or if there's just a tiny syntax error in the file, your entire application is dead.

Crazy easy to protect against. You might as well say not to use JSON files in case there's a syntax error.

 

Do not generate PHP code.

Good advice. OP won't be doing that.

 

File operations like file_put_contents() can actually fail.

Yup. In that case the configuration changes would not be saved.

 

If you write directly to your configuration, you'll end up with garbage and kill your application.

I can't imagine any believable way that would happen with file_put_contents().

 

There can be multiple file operations at the same time. Without concurrency control (e. g. locking), this may also put your configuration into an inconsistent state or make you lose changes.

1. Configuration files are unlikely to have concurrent write operations.

2. file_put_contents() is atomic.

 

Long story short: Use the database.

Except for when you need configuration to connect to it. Or when you don't have a database. Or when the database connection is expensive. Or when there's a risk of database problems. Or when you expect configuration to rarely ever change.
Link to comment
Share on other sites

JSON would be really nice.

 

{
	"subTitle": "Welcome to the Site"
}
// read
$site = json_decode(file_get_contents("/path/to/file.json"), true);

// write
file_put_contents("/path/to/file.json", json_encode($site));
You do lose some of the PHP caching, though. To write out the array to a PHP file, use var_export.

 

 

Thanks, finally got it happening with json after much muttering:

<!DOCTYPE html>
<head lang="en">
	<title>Test JSON array thingy</title>
</head>
<body>
<?php
	$filename = "test.txt";
	$myarray['First'] = "Hello World";
	$myarray['Second'] = "Another World";
	$myarray['Third'] = "Finding Nemo";
	
	// write the array
	file_put_contents($filename, json_encode($myarray));
	
	// test getting that bad boy back
	$txtarray = json_decode(file_get_contents($filename), true);
	
// access title of $book array
	$value = $txtarray['Second'];
	
	echo "<p>$value</p>";

?>
</body>
</html>
Edited by requinix
quoting was a bit mixed up
Link to comment
Share on other sites

Now that you guys have completed your copy-and-paste transaction, I hope we can have a more serious discussion.

 

I agree in principle, but OP isn't generating code. It's about storing data in a file. It just happens that data is being represented with a PHP array.

 

C'mon, requinix. What do you think happens with a file with PHP tags and PHP code in it?

 

So what you're actually saying is: You hope the file will never contain any other code. I hope so too, but hope isn't quite enough.

 

 

 

 

Putting aside the stupid "fairly likely" exaggeration, if someone has the ability to edit files then they wouldn't stop at this one file.

 

So your scripts are all 777, or what? I like your optimism, man. Sounds a bit like the Wordpress mantra: “What could possibly go wrong?”

 

 

 

1. Configuration files are unlikely to have concurrent write operations.
2. file_put_contents() is atomic.

 

What are you talking about? Under what redefinition of atomicity is this statement true?

 

 

Except for when you need configuration to connect to it. Or when you don't have a database. Or when the database connection is expensive. Or when there's a risk of database problems. Or when you expect configuration to rarely ever change.

 

Yes, let's just assume that all people coming to this forum are seasoned experts who write sophisticated applications and know exactly what they're doing.

 

Because PHP programmers don't just blindly copy-and-paste half-working code from forums.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.