选项 1:
您可以使用以下代码制作一个 php 文件:
$command = "mysql --user={$vals['db_user']} --password='{$vals['db_pass']}' "
. "-h {$vals['db_host']} -D {$vals['db_name']} < {$script_path}";
$output = shell_exec($command . '/backup.sql');
恢复数据库并清理您的数据库(在 SQL 文件中需要再次 DROP & CREATE 所有表)。
要删除上传的文件,请执行以下操作:
//The name of the folder.
$folder = 'temporary_files';
//Get a list of all of the file names in the folder.
$files = glob($folder . '/*');
//Loop through the file list.
foreach($files as $file){
//Make sure that this is a file and not a directory.
if(is_file($file)){
//Use the unlink function to delete the file.
unlink($file);
}
}
在一个文件(如 cron.php)中使用它们,然后在您的网站上添加一个按钮以由您的用户手动清除,您也可以将其添加到您的服务器 cronjob 以每小时/每天/每周/每月清除。
选项 2:
如果使用 CPanel - 您需要阅读 cPanel API 2 Functions。
使用 PHP 为用户创建子域,如下代码(API1 示例):
$xmlapi->api1_query($cpanelusr, 'SubDomain', 'addsubdomain', array('user123','bodoamat.com',0,0, '/public_html/NewUserDirectory')
然后复制你的 package.zip 并在 /public_html/NewUserDirectory 中提取它
然后使用cPanel API 2 Functions 创建一个数据库和帐户。
并使用此代码在用户配置文件中添加数据库用户名/密码/名称:
$file_path= 'public_html/NewUserDirectory/config.php';
// Open the file to get existing content
$current = file_get_contents($file_path);
$data_to_write = str_replace("DATABASE_USERNAME",$newdb_user,$current);
$data_to_write = str_replace("DATABASE_PASSWORD",$newdb_pass,$data_to_write);
$data_to_write = str_replace("DATABASE_NAME",$newdb_name,$data_to_write);
file_put_contents($file_path, $data_to_write);
每个用户都有一个子域供测试/使用。
选项 3:
您可以在域的 DNS 服务器中添加此子域(*.domain.ir 到您的 /public_html 目录)。
在您的 PHP 代码中,您必须阅读域 ($_SERVER['REQUEST_URI']),然后为每个用户使用前缀表或其他数据库。 (例如,如果他们打开user1.domain.ir,则使用user_TABLENAME(如果存在)。)并为每个用户创建一个上传目录。