doulipi3742 2015-01-18 11:00
浏览 45
已采纳

如何让用户创建一个新目录,但作为子域而不是PHP?

<?php 

$dirPath = $_POST['username'];
$dirPass = $_POST['password'];

if (file_exists($dirPath)) {
    echo("Not available");
} else {

    if (isset($_REQUEST['sav']))
    {  
        header('Location: http://example.com/'.$dirPath);
    }

    $result = mkdir($dirPath, 0755);

    function recurse_copy($src,$dst) {
        $dir = opendir($src); 
        @mkdir($dst); 
        while(false !== ( $file = readdir($dir)) ) { 
            if (( $file != '.' ) && ( $file != '..' )) { 
                if ( is_dir($src . '/' . $file) ) { 
                    recurse_copy($src . '/' . $file,$dst . '/' . $file); 
                } 
                else
                { 
                    copy($src . '/' . $file,$dst . '/' . $file); 
                } 
            } 
        } 
        closedir($dir); 
    }

    // Source directory (can be an FTP address)
    $src = "copy/";
    // Full path to the destination directory
    $dst = "$dirPath/";

    recurse_copy($src,$dst);
}
?>

Above shows the code that I am following. There is a form with an input field, when the user types something into that input and hits submit, a new directory will be created called 'Whatever he entered in the input' and it will contain all the files from an existing directory called 'Copy'.

This works perfectly. Lets say the user enters 'Example' and hits submit, he would have to go to http://example.com/Example.

How would I make it so that when the new directory is created, the user would have to go to a subdomain to access it. http://Example.example.com instead of http://example.com/Example.

  • 写回答

1条回答 默认 最新

  • douwen3362 2015-01-18 11:13
    关注

    I would do it this way: First configure your webserver that it routes all subdomain traffic to the same webspace. With apache you can do this with a wildcard virtual host entry. E.g.

    <VirtualHost *:80>
      ServerName example.com
      ServerAlias *.example.com
      DocumentRoot /path/to/webroot/
    </VirtualHost>
    

    Second I would create a .htaccess rewrite condition that redirects subdomain requests to the matching folder.

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$
    RewriteRule ^(.*)$ %1/$1 
    

    I have not tested this, but I think it illustrates the concept.

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法