JC99 Posted July 4, 2009 Share Posted July 4, 2009 Hello everyone, I have a file called config.php which has the following in it... <?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? Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/ Share on other sites More sharing options...
.josh Posted July 4, 2009 Share Posted July 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868677 Share on other sites More sharing options...
PFMaBiSmAd Posted July 4, 2009 Share Posted July 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868681 Share on other sites More sharing options...
The Eagle Posted July 4, 2009 Share Posted July 4, 2009 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 $security = "$_GET(); It's highly complex, especially if you're not that well in the PHP language. Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868682 Share on other sites More sharing options...
JC99 Posted July 4, 2009 Author Share Posted July 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868686 Share on other sites More sharing options...
JC99 Posted July 4, 2009 Author Share Posted July 4, 2009 To clarify I want to... <?php $hostname = "localhost"; $database = "database"; $username = "username"; $password = "password"; Use php to add some text here ?> Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868688 Share on other sites More sharing options...
JC99 Posted July 4, 2009 Author Share Posted July 4, 2009 :-) Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868689 Share on other sites More sharing options...
trq Posted July 4, 2009 Share Posted July 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868690 Share on other sites More sharing options...
JC99 Posted July 4, 2009 Author Share Posted July 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868694 Share on other sites More sharing options...
trq Posted July 4, 2009 Share Posted July 4, 2009 Nope. Quote Link to comment https://forums.phpfreaks.com/topic/164730-writing-to-the-middle-of-a-file/#findComment-868696 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.