duanna3634 2015-01-15 03:23
浏览 58
已采纳

如何使用php将文件夹分配给子域

hi i want to assign dynamically created subdomain to a folder after submitting a form.

when the user submits the form, a subdomain and a directory is created. after creating these, how to assign the subdomain to the newly created directory using php.

following is my code:

// creating a subdomain and assigning it to a directory

//Function to create subdomain
function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) {

    //Generate URL for access the subdomain creation in cPanel through PHP
    $buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain .  "&domain=" . $subDomain . "&dir=public_html/subdomains/" . $subDomain;

    //Open the socket
    $openSocket = fsockopen('localhost',2082);
    if(!$openSocket) {
        //SHow error
        return "Socket error";
        exit();
    }

     //Login Details
     $authString = $cPanelUser . ":" . $cPanelPass;

     //Encrypt the Login Details 
      $authPass = base64_encode($authString);

     //Request to Server using GET method
     $buildHeaders  = "GET " . $buildRequest ."
";

     //HTTP
     $buildHeaders .= "HTTP/1.0
";
     //Define Host
     $buildHeaders .= "Host:localhost
";

      //Request Authorization
      $buildHeaders .= "Authorization: Basic " . $authPass . "
";
      $buildHeaders .= "
";

      //fputs
      fputs($openSocket, $buildHeaders);

      while(!feof($openSocket)) {
         fgets($openSocket,128);
      }
     fclose($openSocket);

   //Return the New SUbdomain with full URL
   $newDomain = "http://" . $subDomain . "." . $rootDomain . "/";

   //return with Message
    return "Created subdomain".$newDomain;

}

  //end function to create sub domains


//function to copy files from one directory to the other
function xcopy($source, $dest, $permissions = 0755){
   // Check for symlinks
   if (is_link($source)) {
     return symlink(readlink($source), $dest);
   }

   // Simple copy for a file
  if (is_file($source)) {
     return copy($source, $dest);
  }

   // Make destination directory
   if (!is_dir($dest)) {
     mkdir($dest, $permissions);
   }

   // Loop through the folder
   $dir = dir($source);
     while (false !== $entry = $dir->read()) {
       // Skip pointers
       if ($entry == '.' || $entry == '..') {
          continue;
      }

      // Deep copy directories
      xcopy("$source/$entry", "$dest/$entry", $permissions);
  }

}
 //end function to copy files from directory



//Set the name for your New Sub Domain
$subDomainname="subdomainname";

//cPanel Username
$cPanelUserName="cPanel Username";

//cPanel Password
$cPanelPassName="cPanel Password";

//Main Domain Name
$rootDomainName="maindomainname.com";

$subdomain = $_POST['subdomain'];
$username = $_SESSION['username'];



if(create_subdomain($subDomainname,$cPanelUserName,$cPanelPassName,$rootDomainName)){
  //if sub domain is created

    if(mkdir("$username/$subdomainname",0755)) {
       //if directory is created
       //copy the default file contents from one directory to the other

        if(xcopy("main_directory", "$username/$subdomainname", $permissions = 0755) ){
          //files are not copied
         echo "files are not copied";

        } else {

         //files are copied
         echo "files are copied";

         //HOW TO ASSIGN THE "$username/$subdomainname" directory to the subdomain.example.com??

    }



     } else {
       //if dir is not created
       echo "directory is not created but domain is created";
 }

 } else {
    //if subdomain is not created

    echo "sub domain is not created";
 }
  • 写回答

1条回答 默认 最新

  • dongshao5573 2015-01-15 11:38
    关注

    hi i've found the answer. you just need to give the path in the dir variable in the following codethe the subdomain is assigned to the directory while creating dynamically.

    see the following code:

    $buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain .  "&domain=" . $subDomain . "&dir=public_html/path/to/sub/domain" . $subDomain;
    

    there is no need for htaccess for doing this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?