Page 1 of 1

making new folders...

Posted: Fri Jun 14, 2002 3:22 pm
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.

Posted: Fri Jun 14, 2002 3:36 pm
by Johnm
What type of system are you using? Unix?? A simple shell script could do it.
Direwolf

Posted: Fri Jun 14, 2002 3:37 pm
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

Posted: Fri Jun 14, 2002 10:01 pm
by phice
amnuts: how would I copy all of the files in one folder, to the folder that I just created?

Posted: Fri Jun 14, 2002 10:13 pm
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