Current location: Hot Scripts Forums » Programming Languages » PHP » Write to php file with php script or create new file, it is possible?


Write to php file with php script or create new file, it is possible?

Reply
  #1 (permalink)  
Old 01-28-06, 02:39 PM
Oskare100 Oskare100 is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
Write to php file with php script or create new file, it is possible?

Hello,
I've a config.php file that contains the following:
$dbhost = "localhost";
$dbuname = "root";
$dbpass = "";
$dbname = "test";
$prefix = "test";
$user_prefix = "test";
$dbtype = "MySQL";

And I've a script that gives these variables right values/texts. The problem is that I can't figure out how to insert the new values into the config.php file without having to open it manually and insert the text myself (I'm working on a installation script). Is it possible to insert the right values into the config.php file without editing it manually?

So for example the $dbname = "test"; is changed to the value I writed in the installation script like for example $dbname = "right";.

Or is it possible to just create a new config file with the variables from the script? (the script creates a new config file with the things I want)?

Best Regards
Oskar R
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-28-06, 03:48 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
This should do the trick

PHP Code:

<?php


$string 
'<?php 
$dbhost = "'
$_POST["dbhost"]. '";
$dbuname = "'
$_POST["dbuname"]. '";
$dbpass = "'
$_POST["dbpass"]. '";
$dbname = "'
$_POST["dbname"]. '";
$prefix = "'
$_POST["prefix"]. '";
$user_prefix = "'
$_POST["user_prefix"]. '";
$dbtype = "'
$_POST["dbtype"]. '";
?>'
;

$fp fopen("config.php""w");
fwrite($fp$string);
fclose($fp);

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-28-06, 05:15 PM
Oras Oras is offline
Newbie Coder
 
Join Date: Jan 2006
Location: Baghdad, Iraq
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
nico_swd method is very good, but remember to give 777 permission to config.php
__________________
e-Haw!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 01-28-06, 06:44 PM
Oskare100 Oskare100 is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,
I can't get the values to add, the output (config.php) always looks like this:
PHP Code:

<?php  

$dbhost 
""
$dbuname ""
$dbpass ""
$dbname ""
$prefix ""
$user_prefix ""
$dbtype ""
?>
Without any values, what did I do wrong?
Example script?

Best Regards
Oskar R
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 01-28-06, 07:01 PM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Oras
nico_swd method is very good, but remember to give 777 permission to config.php
Don't give 777 permissions to the config file, give 666 permissions to config file. There is no need to allow the file to be executed.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 01-28-06, 07:01 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Try this

PHP Code:

<?php


if (isset($_POST["Submit"])) {

$string '<?php 
$dbhost = "'
$_POST["dbhost"]. '";
$dbuname = "'
$_POST["dbuname"]. '";
$dbpass = "'
$_POST["dbpass"]. '";
$dbname = "'
$_POST["dbname"]. '";
$prefix = "'
$_POST["prefix"]. '";
$user_prefix = "'
$_POST["user_prefix"]. '";
$dbtype = "'
$_POST["dbtype"]. '";
?>'
;

$fp fopen("config.php""w");
fwrite($fp$string);
fclose($fp);

}

?>
<form action="" method="post" name="install" id="install">
  <p>
    <input name="dbhost" type="text" id="dbhost" value=""> 
    DB Host
</p>
  <p>
    <input name="dbuname" type="text" id="dbuname"> 
    DB Username
</p>
  <p>
    <input name="dbpass" type="password" id="dbpass">
  DB Pass </p>
  <p>
    <input name="dbname" type="text" id="dbname">
  DB Name </p>
  <p>
    <input name="prefix" type="text" id="prefix">
  DB Prefix</p>
  <p>
    <input name="user_prefix" type="text" id="user_prefix">
  Userprefix</p>
  <p>
    <input name="dbtype" type="text" id="dbtype">
  DB Type </p>
  <p>
    <input type="submit" name="Submit" value="Install">
  </p>
</form>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 01-28-06, 08:11 PM
Barnz1986 Barnz1986 is offline
Aspiring Coder
 
Join Date: Jan 2006
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Oskare100
Hello,
I've a config.php file that contains the following:
$dbhost = "localhost";
$dbuname = "root";
$dbpass = "";
$dbname = "test";
$prefix = "test";
$user_prefix = "test";
$dbtype = "MySQL";

And I've a script that gives these variables right values/texts. The problem is that I can't figure out how to insert the new values into the config.php file without having to open it manually and insert the text myself (I'm working on a installation script). Is it possible to insert the right values into the config.php file without editing it manually?

So for example the $dbname = "test"; is changed to the value I writed in the installation script like for example $dbname = "right";.

Or is it possible to just create a new config file with the variables from the script? (the script creates a new config file with the things I want)?

Best Regards
Oskar R
Yes you can but you have to re-write the config file with the new variables in it. Like some of the others have explained above, but you must make sure that the config file matches the original layout etc.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 01-31-06, 03:41 PM
Sheepymot's Avatar
Sheepymot Sheepymot is offline
Newbie Coder
 
Join Date: Jan 2006
Location: England
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

$string = '<?php  

$dbhost 
"'. $_POST["dbhost"]. '" "\n"
$dbuname "'. $_POST["dbuname"]. '" "\n"
$dbpass "'. $_POST["dbpass"]. '" "\n"
$dbname "'. $_POST["dbname"]. '" "\n"
$prefix "'. $_POST["prefix"]. '" "\n"
$user_prefix "'. $_POST["user_prefix"]. '""\n"
$dbtype "'. $_POST["dbtype"]. '""\n"
?>';
That'll give it the same formatting structure.
__________________
PHPGurus - Free PHP Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 02-02-06, 09:32 AM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
What I did with mine..
I made it read each variable in the file, then output boxes for all of them in the order it reads it, or defined categories then sort them...

Take the input, and write to the file using the form input names as the variables, and if you want to add a new, just add it to the end of the file..
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 08-14-08, 06:45 AM
dkazuma dkazuma is offline
New Member
 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
I'm sorry, if we don't have the privilage to access the file permission, how can we write the file?
Also i'm wondering if we can create the file while the directory permission isn't 777. How can we do that??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
PHP File Upload/User Manager Script Needed Computra Script Requests 2 11-09-05 03:39 PM
trying to find a php script to be able to limit inventory in and eshop debidreams HotScripts Site Bug Reports 0 09-10-05 01:36 PM
Need a php developer to create quiz script astrandai Job Offers & Assistance 3 04-18-05 11:30 PM
move files around an ftp server, with php file upload script? wapchimp PHP 2 12-19-04 08:27 AM


All times are GMT -5. The time now is 10:30 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.