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

how to assign a folder to a subdomain using php

您好,我想在提交表单后将动态创建的子域分配给文件夹。

当用户提交表单时,将创建一个子域和一个目录。创建这些后,如何使用 php.

将子域分配给新创建的目录

以下是我的代码:

// 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 ."\r\n";

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

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

      //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";
 }

您好,我找到了答案。您只需要在以下代码中的 dir 变量中提供路径,即在动态创建时将子域分配给目录。

见以下代码:

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

执行此操作不需要 htaccess。