Presto-X Posted October 3, 2008 Share Posted October 3, 2008 Hello guys, I want to make a config.php file and store information like the site title, keyworks database connection settings ext. ext. a lot like Joomla and ZenCart does. I'm guessing something like fopen() but I'm not to sure, I have never played around with editing files with php before. This is the code that I have started with: <?PHP include 'config.php'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?=SITENAME;?></title> </head> <body> <h1><?=SITENAME;?></h1> <form action="" method="POST"> <?PHP fopen("config.php", "r+"); ?> <input name="sitename" type="text" value="<?=SITENAME;?>" style="width:300px;" /><br /> <input name="Save" type="button" value="Save" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/126838-how-do-i-readwrite-to-a-php-file/ Share on other sites More sharing options...
Presto-X Posted October 3, 2008 Author Share Posted October 3, 2008 ok I think I got it working with this: <?PHP if ($_POST['submit']=="Save"){ $myFile = "config.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = ' <?php define("SITENAME","'.$_POST['sitename'].'"); define("SITEURL","'.$_POST['siteurl'].'"); ?>'; fwrite($fh, $stringData); fclose($fh); echo "<h4>Config File Saved!</h4>"; } include 'config.php'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?=SITENAME;?></title> </head> <body> <h1><?=SITENAME;?></h1> <form action="index.php" method="POST"> <input name="sitename" type="text" value="<?=SITENAME;?>" style="width:300px;" /><br /> <input name="siteurl" type="text" value="<?=SITEURL;?>" style="width:300px;" /><br /> <input name="submit" type="submit" value="Save" /> </form> </body> </html> what do you guys think is there a better way I can be doing this? Link to comment https://forums.phpfreaks.com/topic/126838-how-do-i-readwrite-to-a-php-file/#findComment-656073 Share on other sites More sharing options...
JasonLewis Posted October 3, 2008 Share Posted October 3, 2008 Looks okay, you can use file_put_contents() if you want to use PHP5 functions. And if you don't want to use constants you can just use variables. Because you are opening it with single quotes there is no need to escape the $ but if you were using double quotes you would need to escape. $contents = "<?php $variable = 'something'; ?>"; //Results in: <?php = 'something'; ?> $contents = "<?php \$variable = 'something'; ?>"; //Results in: <?php $variable = 'something'; ?> $contents = '<?php $variable = "something"; ?>'; //Results in: <?php $variable = "something"; ?> $contents = '<?php \$variable = "something"; ?>'; //Results in: <?php \$variable = "something"; ?> Link to comment https://forums.phpfreaks.com/topic/126838-how-do-i-readwrite-to-a-php-file/#findComment-656079 Share on other sites More sharing options...
Presto-X Posted October 3, 2008 Author Share Posted October 3, 2008 Hello ProjectFear, Ok that worked out great, is there way to use php to set the file permissions when saving? Warning: fopen(config.php) [function.fopen]: failed to open stream: Permission denied in /var/www/test/index.php on line 5 can't open file. Link to comment https://forums.phpfreaks.com/topic/126838-how-do-i-readwrite-to-a-php-file/#findComment-656140 Share on other sites More sharing options...
JasonLewis Posted October 3, 2008 Share Posted October 3, 2008 chmod() will allow you to change them. Link to comment https://forums.phpfreaks.com/topic/126838-how-do-i-readwrite-to-a-php-file/#findComment-656153 Share on other sites More sharing options...
Presto-X Posted October 3, 2008 Author Share Posted October 3, 2008 Very nice thanks for the help ProjectFear! Link to comment https://forums.phpfreaks.com/topic/126838-how-do-i-readwrite-to-a-php-file/#findComment-656434 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.