Jump to content

How do I read/write to a .php file?


Presto-X

Recommended Posts

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

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?

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"; ?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.