您好我想在提交表单后将动态创建的子域名分配给文件夹。
当用户提交表单时,会创建子域和目录。在创建这些之后,如何使用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";
}
答案 0 :(得分:0)
您好我找到了答案。您只需要在以下代码中给出dir变量中的路径,在动态创建时将子域分配给目录。
请参阅以下代码:
$buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/path/to/sub/domain" . $subDomain;
这样做不需要htaccess。