php script to write to .htaccess file as root or user

mugo

Registered
May 26, 2016
2
0
1
South Africa
cPanel Access Level
Root Administrator
hello everyone! I'm trying to write to a .htaccess of my clients with a php script, so i tried fopen and fwrite but its failing to open the .htaccess on another server/domain but it's only working on the server that contains the script. Is there any way i can achieve this as a root administrator using the user`s username ?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,910
2,216
463
New hello everyone! I'm trying to write to a .htaccess of my clients with a php script, so i tried fopen and fwrite but its failing to open the .htaccess on another server/domain but it's only working on the server that contains the script. Is there any way i can achieve this as a root administrator using the user`s username ?
Hello,

Do you notice a specific error message in the error_log file within the script's document root, or in /usr/local/apache/logs/error_log?

Thank you.
 

mugo

Registered
May 26, 2016
2
0
1
South Africa
cPanel Access Level
Root Administrator
Hello,

Do you notice a specific error message in the error_log file within the script's document root, or in /usr/local/apache/logs/error_log?

Thank you.
Hey Michael.

its giving me this error message

Code:
"Warning: fopen(http://domain.com/.htaccess): failed to open stream: HTTP wrapper does not support writeable connections in....."

Im using this code below:

<?php

$link = array();
$folder = array();

        foreach($_POST['memberid'] as  $value)
        {
         $valu = implode($value);
            if (!empty($valu))
            {
                $link[] = $valu;
                $folder[] = key($value);   
            }  
        }
    
       $length = count($link);

$fp = fopen ("http://domain.com/.htaccess", "r");
for($i =0; $i < $length ; $i++)
{
  if (mkdir($folder[$i], 0755, true)) {

if (!$fp) {
              echo "<p>Unable to write remote file.\n";
               exit;
              }
else{
        fwrite($fp, ''.PHP_EOL.'RedirectMatch 301 /'.$folder[$i].'/(.*) https://'.$link[$i].'/$1');
        echo "file written";
      }
    }
}
fclose($fp);
   
?>
 
Last edited by a moderator: