Author Topic: Writing to the middle of a file  (Read 316 times)

0 Members and 1 Guest are viewing this topic.

Offline JC99Topic starter

  • Irregular
  • Posts: 18
    • View Profile
Writing to the middle of a file
« on: July 03, 2009, 11:52:32 PM »
Hello everyone,

I have a file called config.php which has the following in it...

Code: [Select]
<?php
$hostname 
"localhost";
$database "database";
$username "username";
$password "password";
?>
I am trying to write stuff in between the <?php ?> tags but without deleting what is already in the file.

How would I go about adding text to the middle of a file using PHP?

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,161
  • Grumpy Old Man
    • View Profile
Re: Writing to the middle of a file
« Reply #1 on: July 03, 2009, 11:55:09 PM »
I don't get it.  Are you saying you are trying to open it up and edit it with php, like with file() or something?

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Writing to the middle of a file
« Reply #2 on: July 04, 2009, 12:02:12 AM »
Your code will be much simpler if you just rewrite the whole file. Include()ing the file will give you the current values in the indicated variables. Just replace any that you want to change when you write the file out.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline The Eagle

  • Enthusiast
  • Posts: 128
  • Gender: Male
  • I can assist you!
    • View Profile
    • Vazzer
Re: Writing to the middle of a file
« Reply #3 on: July 04, 2009, 12:02:51 AM »
You should not mess with the configuration set in this file, this is helping you identify your MySQL server / login information.

$hostname is the address to your MySQL server.
$database is the name of the MySQL database the application will use.
$username is the username you most-likely log in with.
$password is the password you most-likely use to log in.

If you're looking to add more fields, which is highly unneeded and may potentially make this application not work, you would have to identify the variables such as
Code: [Select]
$security = "$_GET();
It's highly complex, especially if you're not that well in the PHP language.
- E-mail me at maniac@vazzer.net

Offline JC99Topic starter

  • Irregular
  • Posts: 18
    • View Profile
Re: Writing to the middle of a file
« Reply #4 on: July 04, 2009, 12:12:32 AM »
I don't get it.  Are you saying you are trying to open it up and edit it with php, like with file() or something?

Something like using fopen() to open the file with PHP then somehow add text below what is already in the file but before the closing ?> tag.
« Last Edit: July 04, 2009, 12:13:15 AM by JC99 »

Offline JC99Topic starter

  • Irregular
  • Posts: 18
    • View Profile
Re: Writing to the middle of a file
« Reply #5 on: July 04, 2009, 12:16:30 AM »
To clarify I want to...
Code: [Select]
<?php
$hostname 
"localhost";
$database "database";
$username "username";
$password "password";
Use 
php to add some text here
?>
« Last Edit: July 04, 2009, 12:26:49 AM by JC99 »

Offline JC99Topic starter

  • Irregular
  • Posts: 18
    • View Profile
Re: Writing to the middle of a file
« Reply #6 on: July 04, 2009, 12:27:16 AM »
:-)
« Last Edit: July 04, 2009, 12:27:51 AM by JC99 »

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,256
    • View Profile
Re: Writing to the middle of a file
« Reply #7 on: July 04, 2009, 12:28:56 AM »
So you basically just need to add text to the end of the file then. The closing ?> is not needed by php if you file contains php only, so just remove it and get on with the job.

Offline JC99Topic starter

  • Irregular
  • Posts: 18
    • View Profile
Re: Writing to the middle of a file
« Reply #8 on: July 04, 2009, 12:32:37 AM »
So you basically just need to add text to the end of the file then. The closing ?> is not needed by php if you file contains php only, so just remove it and get on with the job.

Ok, good to know, thanks...

Just one other question, if I don't add the closing ?> but then include() or require() the file will it cause problems?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,256
    • View Profile
Re: Writing to the middle of a file
« Reply #9 on: July 04, 2009, 12:34:14 AM »
Nope.