writing to an array into a php file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dmallia
Forum Commoner
Posts: 25
Joined: Sat Nov 19, 2011 3:18 pm

writing to an array into a php file

Post by dmallia »

So i did a game servers listing script and have a file servers.php with 2 arrays in it

Code: Select all

$servers = array('1' => 'ip','2' => 'ip','3' => 'ip',);
$ports = array('1' => 'port','2' => 'port','3' => 'port',);
I would like a script to insert data into that arrays in the file servers.php and save it. I know that there is the fwrite() but don't know if it writes to a php file. I already did the form.

Code: Select all

<form method="post" enctype="multipart/form-data">
<table>
<tr>
	<td align="center"><label>IP</label></td>
    <td align="center"><label>Port</label></td>
    <td></td>
</tr>
<tr>
	<td><input type="text" name ="IP"></td>
    <td><input type="text" name="Port"></td>
    <td><input type="submit" name ="Add"value="Add"></td>
</tr>
</table>
</form>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: writing to an array into a php file

Post by s.dot »

I'm not quite sure what you're trying to do.

Have a look at the serialize() and unserialize() functions for storing arrays as strings.

http://www.php.net/serialize
http://www.php.net/unserialize
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dmallia
Forum Commoner
Posts: 25
Joined: Sat Nov 19, 2011 3:18 pm

Re: writing to an array into a php file

Post by dmallia »

i have 2 arrays with data in them in servers.php.

Code: Select all

$server = array (
                         '1' => 'ip',
                         '2' => 'ip',
                         '3' => 'ip',);
$port = array (
                     '1' => '28960',
                     '2' => '28961',
                     '3' => '28970',
                      );
I need a script that has to take the ip and port from the text boxes and automatically adds this data to the file server.php and the file becomes as shown below.

Code: Select all

$server = array (
                         '1' => 'ip',
                         '2' => 'ip',
                         '3' => 'ip',
                         '4' => 'ip',
                          );
$port = array (
                     '1' => '28960',
                     '2' => '28961',
                     '3' => '28970',
                     '4' => '28910',
                      );
Post Reply