We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,644 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Managing folder structures using PHP

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.

Thanks!

2
Contributors
4
Replies
3 Hours
Discussion Span
8 Months Ago
Last Updated
5
Views
Question
Answered
mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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.

diafol
Keep Smiling
Moderator
10,846 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,539
Skill Endorsements: 61

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?

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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.

Example
Your profile.php page:

session_start();
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...
}else{
    ...redirect page to index or have a dedicated error message...
}

include('templates/headhtml.php');
include('templates/profileTPL.php');
include('templates/footer.php');

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.

diafol
Keep Smiling
Moderator
10,846 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,539
Skill Endorsements: 61

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

mmcdonald
Junior Poster in Training
92 posts since Sep 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 8 Months Ago by diafol

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0650 seconds using 2.65MB