Page 1 of 1

Copying static content and making it dynamic.

Posted: Sat Nov 20, 2010 3:03 pm
by spedula
I'm writing an application where once a user registers on my domain, a subdomain is created for them automatically. Kind of like blogger.com This part I have written out correctly. I'm kind of stuck at this next part here.

I need a way to populate this users subdomain/subdirectory with dynamic content specific to that use. I wrote a bit of code that just loops through another subdirectory, called template, and copies all the files from there into the users subdomain. This then leaves me with the issue of how I can make this newly copied static content dynamic. I've come up with one solution that opens the index.php file after copying it over and adds a $usrname = at the top.

Here is the code:

Code: Select all

<?
function copydir($source,$destination) {
	if(!is_dir($destination)){
		$oldumask = umask(0); 
		mkdir($destination, 01777); // so you get the sticky bit set 
		umask($oldumask);
	}
	$dir_handle = @opendir($source) or die("Unable to open");
	while ($file = readdir($dir_handle)) {
		if($file!="." && $file!=".." && !is_dir("$source/$file")){
			copy("$source/$file","$destination/$file");
		}
	}
	closedir($dir_handle);
}

if(!file_exists("../".$rowdata[storename]."/index.php")) {
	copydir("../template","../".$rowdata[storename]);
	copydir("../template/css","../".$rowdata[storename]."/css");
	copydir("../template/css/images","../".$rowdata[storename]."/css/images");
	$newFile = "../".$rowdata[storename]."/index.php";
	$fh = fopen($newFile, 'c') or die("can't open file");
	$data = '<? $usrname="'.$username.'" ?>';
	fwrite($fh, $data);
	fclose($fh);
	header('location: template.php');
}
?>

This kind of leaves me in a bad place, since if the user ever wants to change their template, then I have to write a script that deletes the files from their subdirectory and the copies over the new ones from the different template. It just seems a little too much and not efficient to me at all, especially because there will be a small window where this user doesn't have any content on their site at all.

I'm interested in what others think of this or if these is a better way of achieving what I need.

Thanks.

Re: Copying static content and making it dynamic.

Posted: Sat Nov 20, 2010 3:40 pm
by Celauran
What is the purpose of these templates? What do they do?

Re: Copying static content and making it dynamic.

Posted: Sat Nov 20, 2010 3:49 pm
by spedula
The concept is that each user will have their own site create with the help of the backend of our site. Kind of like a CMS, but limited. Each site will kind of be like a micro-ecommerce site in a very specific niche that I can't divulge.

Re: Copying static content and making it dynamic.

Posted: Sat Nov 20, 2010 3:53 pm
by Celauran
What I was getting at was whether the templates change anything more than the appearance and layout of the page. Do they add functionality, for instance? Rather than moving around entire directories, I'm wondering if you can't keep track of, say, which stylesheet to call, or which modules to load.

Re: Copying static content and making it dynamic.

Posted: Sat Nov 20, 2010 4:03 pm
by spedula
Each site will have the same functionality as the others. It's more of a presentation matter than anything else really.