PHP - Username to Folder [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

PHP - Username to Folder [SOLVED]

Post by tecktalkcm0391 »

I know to use mkdir(); to make a folder but I want to do a little more that that.
This is what I want to do:

When a user signs-up, a folder is created at domain.com/profiles/[the username]
and a standard php file is copied there from domain.com/profiles/defaultprofile.php
but the file is renamed to index.php. After that is done I want to make another folder at
domain.com/~[username], and then create another php file, which has a header which redirects
to domain.com/profiles/[the username].... which if i need to make another default page I can, but
I don't know how to make it so that it would foward to the right directory.

How would I do this all...
Last edited by tecktalkcm0391 on Mon Jun 12, 2006 6:39 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It seems like rather than creating directories and files, you could just use mod_rewrite and a Front Controller to do the same thing. Then you could keep the user's site data in a database which would probably be much easier to manage.
(#10850)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ok, yeah all of the data is already in a database, its like a public profile is what I was aiming to do.
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

I had this issue a while ago.... here's a possible solution:

viewtopic.php?t=40707&highlight=
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

aborint has the best idea.

If you want users to access their profiles from a folder just have mod_rewrite redirect yourdomain.com/theusername to yourdomain.com/profile.php?user=theusername and then grab the username using $_GET and show the correct profile on the page.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

Code: Select all

mkdir($username);
$filename = $username.'/index.php';
$somecontent = "<?\n".'header("Location: domain.com/profiles/'.$username.'")'."\n?>\n";
   if (!$handle = fopen($filename, 'x')) {
         echo "Cannot open file ($filename)";
         exit;
   }
   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
  
   echo "created profile for ".$username;
  
   fclose($handle);
I think this should do it, as per usual completely untested and written straight into the post 8O
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I'll try it. Thanks!
Post Reply