/    Sign up×
Community /Pin to ProfileBookmark

PHP File System to Change Array Values

This has been bugging me for quite some time now, and I was wondering if any of you guys could help.

I’ve been trying to figure out how to change the value of variables inside an array using the PHP file system (like fopen or file_get_contents). And of course I need to be able to save the file when I’m through changing values. What makes it hard, is that I can’t get the variable by line number as it’s subject to change.

Is this even possible?

Thanks,
Scotty

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 18.2010 — Not 100% sure what you mean. Perhaps you could use an array along with serialize() and unserialize()? Or perhaps you could use an .ini file format along with the parse_ini_file() function?
Copy linkTweet thisAlerts:
@ScottyBoyauthorNov 18.2010 — Okay, let me give you an example then.

Lets say I have this form:
<i>
</i>&lt;form action="update.php" action="post"&gt;
&lt;input type="text" name="input" /&gt;
&lt;input type="text" name="input2" /&gt;
&lt;/form&gt;


And I want to update the corresponding variables inside an array (which may already have values):
<i>
</i>$form_values = array(
'input' =&gt; 'value',
'input2' =&gt; 'value2'
);


I realize that I could use the global $_POST variable, but that's only good for one submission and it's gone. I need to somehow add the inputs into an array and save the file. I know you're probably thinking "Why doesn't he just use MySQL?". I'm planning on using this method to set MySQL connection variables in the future. i.e, define('DB_PASS', 'myPassword');. So as you can see, I can't add the values to a DB table if I haven't connected to one.

Thanks again,

Scotty
Copy linkTweet thisAlerts:
@NogDogNov 18.2010 — How about PHP sessions with session_start() and the $_SESSION array?
Copy linkTweet thisAlerts:
@ScottyBoyauthorNov 18.2010 — That would be good, except I need the changes to be permanent. And show for everyone, not just people who have submitted the form.

Have you hear of Simple Machines forum software? They somehow manage to do this with their installer script. I have looked at the source, but if confused me to death.
Copy linkTweet thisAlerts:
@criterion9Nov 18.2010 — You might have a look at SimpleXml to see if it might suit your needs.
Copy linkTweet thisAlerts:
@ScottyBoyauthorNov 18.2010 — Thank you. I'll try this later when I have a chance.
Copy linkTweet thisAlerts:
@ehimeNov 19.2010 — Maybe?

[code=html]
<form action="update.php" action="post">
<input type="text" name="input" />
<input type="text" name="input2" />
</form>
[/code]


[code=php]
<?php

// update.php
$file = 'your.txt';
$form_values = array(
'input' => $_get['input'],
'input2' => $_get['input2'],
);

file_put_contents($file, $form_values, FILE_APPEND | LOCK_EX);

?>
[/code]
×

Success!

Help @ScottyBoy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...