|
|
|
PHP File read/write??????
|
Original Message
|
Name: Alirezan1
Date: January 14, 2005 at 00:03:15 Pacific
Subject: PHP File read/write??????OS: MDK Linux 10.1 OECPU/Ram: AMD |
Comment: 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
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Laler
Date: January 14, 2005 at 07:00:43 Pacific
Subject: PHP File read/write?????? |
Reply: (edit)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
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: Alirezan1
Date: January 14, 2005 at 11:15:23 Pacific
Subject: PHP File read/write?????? |
Reply: (edit)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
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: FBI Agent
Date: January 14, 2005 at 13:24:45 Pacific
Subject: PHP File read/write?????? |
Reply: (edit)<?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 AgentAIM: EliteAssassin187
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Laler
Date: January 14, 2005 at 19:54:12 Pacific
Subject: PHP File read/write?????? |
Reply: (edit)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=newLineHere I 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
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: Alirezan1
Date: January 14, 2005 at 23:36:22 Pacific
Subject: PHP File read/write?????? |
Reply: (edit)Thanks everyone! especially Laler for providing a very useful code. Ali AlirezaAlireza@arnit.net
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|