Hey all,

I have a folder called 'templates' in my root dir (../templates/). When a PHP form is submitted I want to copy a folder out of 'templates' called 'template1' (../templates/template1) and paste it into the root directory but with the new name of 'username123'.

How can I copy, rename and paste a folder from one dir to another using PHP?

I have read the manuals for copy and rename but some snippets (non working examples are cool too) would be really useful.


4 Years
Discussion Span
Last Post by mmcdonald

This sounds like needless duplication. Can't you do what you need without copying files? This is never a good approach. Maintenance is a pain.


This is a website where the user registers thieir own directory such as directory.com/username and that is their own profile area. When they register they first choose a template and this is copied and renamed to their username. Then they completea few forms to customise their own profile.

Better methods?


Keep templates general and user settings/values in a DB. The fact that you're using the /username can just translate to .htaccess mod rewrite for profile.php?username=xxxx

I'm assuming that your template pages have placeholders - either vanilla php or built on a templating engine something like Smarty, Twig or RainTPL? If so, no need (IMO) to copy these files.

Your profile.php page:

if(isset($_GET['username']) && isset($_SESSION['user_id'])){
    ...check DB to see if username exists and this is the user that's logged in...
    ...if so - get all user settings (values for placeholders) from DB...
    ...redirect page to index or have a dedicated error message...


Your profileTPL.php file:

<link href="/assets/css/<?php echo $css;?>.css" rel="stylesheet">
<h2><?php echo $username;?>'s Page</h2>

OR for certain templating engines, maybe something like:

<h2>{username|raw}'s Page</h2>

You may need .htaccess to 'prettify' your urls.

That's a simplistic solution, but I hope it gives you an idea of where I was coming from.


Okay thanks, I'll probably head down this route. I had it on paper but chose the folder structure method

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.