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.

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

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。