Using fopen to modify the htaccess file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devain
Forum Commoner
Posts: 25
Joined: Wed May 10, 2006 9:00 pm
Contact:

Using fopen to modify the htaccess file

Post by devain »

Hello


Anyone have any suggestions on how to modify the htaccess file using fopen Here is my short piece of code that should open and modify the access file but it does not work I am assuming that the page is running as the wrong user to modify the file (hence NOBODY). I am wondering how I can change this section of code so I can modify this file automatically.

Code: Select all

$ourFileNameAccess = "/home/USERNAME/public_html/.htaccess";
$fh2 = fopen($ourFileNameAccess, 'a') or die("can't open file");




$stringData2 = "RewriteRule ^([^/]+)/$toplevel_name.hml$ MainTemplate/$toplevel_name.php?menu=$1\n\n\n";
fwrite($fh2, $stringData2);


fclose($fh2);

Thanks for any Input;


Devain
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Re: Using fopen to modify the htaccess file

Post by xpgeek »

devain wrote:Hello


Anyone have any suggestions on how to modify the htaccess file using fopen Here is my short piece of code that should open and modify the access file but it does not work I am assuming that the page is running as the wrong user to modify the file (hence NOBODY). I am wondering how I can change this section of code so I can modify this file automatically.

Code: Select all

$ourFileNameAccess = "/home/USERNAME/public_html/.htaccess";
$fh2 = fopen($ourFileNameAccess, 'a') or die("can't open file");




$stringData2 = "RewriteRule ^([^/]+)/$toplevel_name.hml$ MainTemplate/$toplevel_name.php?menu=$1\n\n\n";
fwrite($fh2, $stringData2);


fclose($fh2);

Thanks for any Input;


Devain
Try it.
$rules - rewrite rule;
$filename - path to .htaccess;

Code: Select all

$hta = file_get_contents($filename);
$hta .= "\n".$rules;
file_put_contents($filename, $hta);
Post Reply