Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
From time to time, I need to read a fileon my website and add some stuff in between other data. Also I need to do it in PHP. Can somebody help me do this please?
Here is the file I want to open and add stuff to:
<?php $GLOBALS["users"]=array(
array("A","AA","D","C",0,"^",3,1),
array("B","BB","D","C",1,"",7,1),
array("C", "CC", "D", "C",0,"^",3, 1 ),
); ?>I want to add same structure of ARRAYS before "); ?>" but as it seems, I am unable to do so. Can anybody show me a way to do this or give me a piece of code to do that?
thanks.
AlirezaAlireza@arnit.net

am not sure on what you really want, do you want to know how fopen(), fread() & fwrite() (functions needed to work on flat database) works? or just want to know how to put a new array between/before/after a set of arrays?
^o^
are you in Asia? do you watch Animax Asia? Please Vote

Hi,
Thanks for the reply and sorry about the bad question.
Well, I elaborate more on the problem:I have a flat file: /XXX/YYY/config.php
it has stuff I wrote above in it:<?php $GLOBALS["users"]=array(
array("A","AA","D","C",0,"^",3,1),
array("B","BB","D","C",1,"",7,1),
array("C", "CC", "D", "C",0,"^",3, 1 ),
); ?>the structure of the file is the same for every entry I have to add in the file each time I would update it.
I have to add:
"array ( something,something,something,something,something,something);"I need to add this line either after <?php $GLOBALS["users"]=array(
or before: ); ?>
How can I do this in PHP?
I hope I clarified the problem enough this time ;)
Thanks anyways,
AlirezaAlireza@arnit.net

<?php $GLOBALS["users"]=array(
array("A","AA","D","C",0,"^",3,1);
array("B","BB","D","C",1,"",7,1);
array("C", "CC", "D", "C",0,"^",3, 1 );array("something","something","something");
); ?>
i have NO idea what you're saying but if this is what you're looking for, here ya go (im pretty sure it could be. you wanted to add another array in the $globals array, right?) anyway, here ya go. good luck!FBI Agent
AIM: EliteAssassin187

I still can't imagine the purpose :D but to add/modify a flatfile, you should leave some pattern unchanged...
in this case we can use \n (new line) as the separator
so, you're right about creating the initial config.php like this:
<?php
$GLOBALS["users"] = array(
array("A", "AA", "D", "C", 0, "^", 3, 1),
array("B","BB","D","C",1,"",7,1),
array("C", "CC", "D", "C", 0, "^", 3, 1),
);
?>and here's the script
<?php
// the new array
$newArray = 'array("B", "BB", "D", "C", 1, "", 7, 1),';// open file for reading
$opFile = fopen ("config.php", "r");// put the whole content to $handle
$handle = fread ($opFile, filesize ("config.php"));
fclose ($opFile);// uncomment and move the echo() below to the bottom of each new command you made (for easier debugging). you can also change $handle to $modifiedArray later
//echo '<pre>'.htmlspecialchars (print_r ($handle, TRUE)).'</pre>';// next part, break by lines
$handle = explode ("\n", $handle);// count the lines
$i = count ($handle);// assuming you're going to put the new array to the first line
$modifiedArray = array();
for ($n = 0 ; $n < $i ; $n++){
$modifiedArray[] = $handle[$n];
// line #3 or array key [2] is where you want the new array, so after key [1]:
if ($n == 1){
// put the new entry
$modifiedArray[] = $newArray;
}
}// implode
$handle = implode ("\n", $modifiedArray);// put back to file
$opFile = fopen ("config.php", "w");
fwrite ($opFile, $handle);
fclose ($opFile);
?>I've tested it so it should work (if that's what you really want) :D
here's the test:
http://iix.broadband.or.id/foo.php?new=newLineHereI made $newArray = $_GET['new'];
and config.txt so it's viewable...
don't forget to chmod config.php to allow write...
^o^
are you in Asia? do you watch Animax Asia? Please Vote

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |