The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'General Discussion' started by mugo, Jun 21, 2016.

  1. mugo

    mugo Registered

    Joined:
    May 26, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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 ?
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    648
    Trophy Points:
    113
    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.
     
  3. mugo

    mugo Registered

    Joined:
    May 26, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    South Africa
    cPanel Access Level:
    Root Administrator
    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);
       
    ?>
    
    
     
    #3 mugo, Jun 23, 2016
    Last edited by a moderator: Jun 23, 2016
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    648
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page