Writing to a file

View: New views
10 Messages — Rating Filter:   Alert me  

Writing to a file

by JasonCarson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello everybody,

How would I go about writing stuff to a file but in between the <?php ?>
tags?

Example, say I have config.php as follows...

<?php

$hostname = "localhost";
$database = "database";
$username = "username";
$password = "password";

?>

How would I go about adding stuff at the end of the file, but before the
?> tag?


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by pscott :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jason Carson wrote:
> How would I go about writing stuff to a file but in between the <?php ?>
> tags?
>

http://www.php.net/file_put_contents

-- Paul

http://www.paulscott.za.net/
http://twitter.com/paulscott56
http://avoir.uwc.ac.za

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by Stuart-47 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/3 Jason Carson <jason@...>:

> Hello everybody,
>
> How would I go about writing stuff to a file but in between the <?php ?>
> tags?
>
> Example, say I have config.php as follows...
>
> <?php
>
> $hostname = "localhost";
> $database = "database";
> $username = "username";
> $password = "password";
>
> ?>
>
> How would I go about adding stuff at the end of the file, but before the
> ?> tag?

Remove the ?> tag - it's not required if it's the last thing in the file.

-Stuart

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by Sándor Tamás (HostWare Kft.) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The classic method is:
1.) read the whole file into an array of strings.
2.) search for the last line
3.) insert a string BEFORE the last line.
4.) write back the whole file to the same name

Classic file handling does allow you to append to the end of a file, or
recreate as a whole.
Usually these types of editing must be done in memory, because file handling
does not allow inserts, deletes.

SanTa

----- Original Message -----
From: "Jason Carson" <jason@...>
To: <php-general@...>
Sent: Friday, July 03, 2009 12:01 PM
Subject: [PHP] Writing to a file


> Hello everybody,
>
> How would I go about writing stuff to a file but in between the <?php ?>
> tags?
>
> Example, say I have config.php as follows...
>
> <?php
>
> $hostname = "localhost";
> $database = "database";
> $username = "username";
> $password = "password";
>
> ?>
>
> How would I go about adding stuff at the end of the file, but before the
> ?> tag?
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by Michael A. Peters :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jason Carson wrote:

> Hello everybody,
>
> How would I go about writing stuff to a file but in between the <?php ?>
> tags?
>
> Example, say I have config.php as follows...
>
> <?php
>
> $hostname = "localhost";
> $database = "database";
> $username = "username";
> $password = "password";
>
> ?>
>
> How would I go about adding stuff at the end of the file, but before the
> ?> tag?
>
>

IMHO that's kind of dangerous.
Better to have an xml file -

<config>
<dbconfig>
   <hostname value="localhost" />
   <database value="database" />
   <username value="username" />
   <password value="password" />
</dbconfig>
</config>

and read it into a DOM object via DOMDocument.
Then you can add things to the DOM
by making them children of the config node - and write the whole dom
back to file.

That way your web app does not have write permission to a file that gets
executed, and you can potentially cache the DOMDocument object in memory
so it doesn't have to read from file every page load to get your
settings (not tried that yet myself but I don't see why you couldn't).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by Daniel Brown-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Jul 3, 2009 at 06:01, Jason Carson<jason@...> wrote:
> Hello everybody,
>
> How would I go about writing stuff to a file but in between the <?php ?>
> tags?

    The current industry standard is a combination of text editor and
keyboard, but there are many options.

    More specifically, are you trying to write to a PHP file *with*
PHP?  And if so, would this be the kind of file you're trying to write
(such as creating an automated installer with auto-config)?

--
</Daniel P. Brown>
daniel.brown@... || danbrown@...
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by Daniel Brown-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/3 Sándor Tamás (HostWare Kft.) <sandortamas@...>:

> The classic method is:
> 1.) read the whole file into an array of strings.
> 2.) search for the last line
> 3.) insert a string BEFORE the last line.
> 4.) write back the whole file to the same name
>
> Classic file handling does allow you to append to the end of a file, or
> recreate as a whole.
> Usually these types of editing must be done in memory, because file handling
> does not allow inserts, deletes.

    Please don't top-post in threads.  It makes them difficult to read
for folks searching the archives.

--
</Daniel P. Brown>
daniel.brown@... || danbrown@...
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by Shawn McKenzie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jason Carson wrote:

> Hello everybody,
>
> How would I go about writing stuff to a file but in between the <?php ?>
> tags?
>
> Example, say I have config.php as follows...
>
> <?php
>
> $hostname = "localhost";
> $database = "database";
> $username = "username";
> $password = "password";
>
> ?>
>
> How would I go about adding stuff at the end of the file, but before the
> ?> tag?
>

The way I've done this in the past is to use an array.  Then I can
include the file, change the array and then var_export() to the file:

//config.php
<?php

$config = array (
  'hostname' => 'localhost',
  'database' => 'database',
  'username' => 'username',
  'password' => 'password',
);


//file that does the changing writing
<?php

include('config.php');

$config['database'] = 'otherdb';
$config['username'] = 'jason';
$config['password'] = 'abcdefg123';

$content = "<?php\n".'$config = '.var_export($config, true).";\n?>";
file_put_contents('config.php', $content);

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by JasonCarson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> On Fri, Jul 3, 2009 at 06:01, Jason Carson<jason@...> wrote:
>> Hello everybody,
>>
>> How would I go about writing stuff to a file but in between the <?php ?>
>> tags?
>
>     The current industry standard is a combination of text editor and
> keyboard, but there are many options.
>
>     More specifically, are you trying to write to a PHP file *with*
> PHP?  And if so, would this be the kind of file you're trying to write
> (such as creating an automated installer with auto-config)?
>
> --
> </Daniel P. Brown>
> daniel.brown@... || danbrown@...
> http://www.parasane.net/ || http://www.pilotpig.net/
> Check out our great hosting and dedicated server deals at
> http://twitter.com/pilotpig
>

Yes, I am trying to write stuff to a file with PHP but in between the
<?php ?> tags and without deleting what is already in the file.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Writing to a file

by Michael A. Peters :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jason Carson wrote:

>
> Yes, I am trying to write stuff to a file with PHP but in between the
> <?php ?> tags and without deleting what is already in the file.
>
>

The only way is to read the file into memory, manipulate it there, and
then rewrite it.

Use a regex to get rid of the last closing tag, append your new lines to
  it, and then write it.

You probably want to do it atomic or you can end up with occasional
issues (broken pages that requested the file you are writing before it
is finished being written) and I don't know if php can do that.

Again that's where using a non php config file that is parsed by php.
You can make your edits, save the file to cache (IE APC) and then write
it. Files that need its contents can get it out of cache and only need
to read it from file if it isn't cached. Since you cache it before
writing, it will be in cache while it is being written.

Another advantage to not using php for the file, you can validate the
values before setting/using them and handle problems appropriately in
your code rather than have a completely broken site because the script
that wrote the file had a bug causing a ; not to be written under
certain conditions.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php