making new folders...

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
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

making new folders...

Post by phice »

Lets say I've got a users table in mysql... I would want each user to have their own folder, in example: http://www.mysite.com/users/[b]username[/b]/*.*, where *.* are all the files that are inside that certain folder.
Image Image
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

What type of system are you using? Unix?? A simple shell script could do it.
Direwolf
User avatar
amnuts
Forum Newbie
Posts: 16
Joined: Fri Jun 14, 2002 1:48 pm
Contact:

Post by amnuts »

Firslty make sure that 'users' is readble/writeable by the web server. (use chmod from the shell to alter this).

Than do something like this:

Code: Select all

<?php

$path = "/usr/home/foo/www/users";

$id = @mysql_connect($host,$user,$pass) or 
	die("Unable to connect to mysql server: $host");
@mysql_select_db($db,$id) or
	die("Unable to select database: $db");
$result = @mysql_query("SELECT * FROM table",$id) or die("Unable to perform query");
while ($row = @mysql_fetch_array($result,MYSQL_ASSOC))
&#123;
	if (!(mkdir("&#123;$path&#125;/&#123;$row&#1111;'username']&#125;",0777)))
		echo "<p>Could not create directory for &#123;$row&#1111;'username']&#125;</p>\n";
&#125;
@mysql_free_result($result);

?>
Andy
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

amnuts: how would I copy all of the files in one folder, to the folder that I just created?
Image Image
User avatar
amnuts
Forum Newbie
Posts: 16
Joined: Fri Jun 14, 2002 1:48 pm
Contact:

Post by amnuts »

phice wrote:amnuts: how would I copy all of the files in one folder, to the folder that I just created?
Well, that depends... If you have access to run system commands through PHP then you could choose a method like:

system("cp /foo/bar/* {$path}/{$row['username']}/");

or

$return = `cp /foo/bar/* {$path}/{$row['username']}/';

If you don't, then you'll have to do it the long way round and use opendir, go through each file and copy it individually. If you want a demo of that I can post it up.

Andy
Post Reply