Computing.Net > Forums > Web Development > PHP File read/write??????

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP File read/write??????

Reply to Message Icon

Name: Alirezan1
Date: January 14, 2005 at 00:03:15 Pacific
OS: MDK Linux 10.1 OE
CPU/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.


Alireza

Alireza@arnit.net



Sponsored Link
Ads by Google

Response Number 1
Name: Laler
Date: January 14, 2005 at 07:00:43 Pacific
Reply:

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


0

Response Number 2
Name: Alirezan1
Date: January 14, 2005 at 11:15:23 Pacific
Reply:

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,


Alireza

Alireza@arnit.net


0

Response Number 3
Name: FBI Agent
Date: January 14, 2005 at 13:24:45 Pacific
Reply:

<?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



0

Response Number 4
Name: Laler
Date: January 14, 2005 at 19:54:12 Pacific
Reply:

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


0

Response Number 5
Name: Alirezan1
Date: January 14, 2005 at 23:36:22 Pacific
Reply:

Thanks everyone! especially Laler for providing a very useful code.

Ali

Alireza

Alireza@arnit.net


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP File read/write??????

SSIs can't read/write subdirectory www.computing.net/answers/webdevel/ssis-cant-readwrite-subdirectory/2001.html

Saving a form to a php file www.computing.net/answers/webdevel/saving-a-form-to-a-php-file/3154.html

php files...what do i do?!?! www.computing.net/answers/webdevel/php-fileswhat-do-i-do/210.html