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.


Recommended Answers

All 4 Replies

Member Avatar for diafol

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?

Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.