Forums

PHP

This topic is locked

Upload files in a directory

Posted 11 Apr 2002 09:48:26
1
has voted
11 Apr 2002 09:48:26 kevin kevink posted:
How do i make the upload file to each users directory.. let say i have a users login and that users be able to upload to their directory only and not the root directory.. so if a new users sign up they will get their own directory to upload their image to..

Take care...

Replies

Replied 11 Apr 2002 17:41:30
11 Apr 2002 17:41:30 Eric Kaiser replied:
I just went through this myself. When users register with the site a directory is made for them based on the UserID autogenerated by the mySQL database. (Let me know if you need more details on that).
When users login with username and pwd generated at registration they are redirected to welcome page, which has restricted Access behavior applied, this is very important. When you login in UD using PHP a session variable called KT_Username is generated. By grabing the KT_Username I can now query the database for the UserID which is also the directory where files need to be uploaded to. A cookie is set to the value of the UserID. From the welcome page there is a link to the upload page. On the upload page I applied the PHP Upload behavior, then the Insert behavior. I then modified the Upload code to upload to the user's directory, which is the value of the cookie (which is their UserID).
Go into code view and look for a block of code that looks like this:

if (is_uploaded_file($file)) {
if (($tgCHeader <= $tg_FileSize)&&(filesize($file_name) <= $tg_FileSize)||(!$tg_OptionCheckSize)) {
$tg_Root=dirname($HTTP_SERVER_VARS["PATH_TRANSLATED"])."/";
$tg_Dir="../users/$UserID/";
if ($tg_Dir=="/" {
$tg_Dir="";
}

The line of code that is important here is: $tg_Dir="../users/$UserID/";

This is my code, yours will look different. But notice that I have inserted the UserID value which is declared on the page to the cookie.

Hopefully this made sense to you. But it works pretty good.

Good luck!

Replied 11 Apr 2002 19:16:20
11 Apr 2002 19:16:20 kevin kevink replied:
Can you explain to me how the users directory are created when they sign up? i don't know that part.. what script to do that?
Replied 12 Apr 2002 18:41:27
12 Apr 2002 18:41:27 Eric Kaiser replied:
It's a two step process. After you have inserted the users information in the database, you need to get the auto incremented UserID from for the database. (I am using mySQL and hope you are to). To do this with PHP put in this line of code AFTER the line of code that does the INSERT:

$id = mysql_insert_id();

that is all that is needed to get the last inserted ID number. The next thing we need to do is create a directory based on that ID number. We can do that in the next line by doing this:

mkdir( "$id", 0777 );

That line of code will make a directory based on the ID number and will give it permissions of 777.

I used UltraDevs basic Insert behavior to insert my registration information. And then I just inserted those couple of lines after the insert. Here is the entire block of code from the insert on:

$MM_editQuery = "insert into " . $MM_editTable . " (" . $MM_tableValues . " values (" . $MM_dbValues . "";
if ($MM_abortEdit!=1) {
// execute the insert
$queryrs = $NewsDesigner->Execute($MM_editQuery) or DIE($NewsDesigner->ErrorMsg());
$id = mysql_insert_id();
mkdir( "users/$id", 0777 );
if ($MM_editRedirectUrl) {
header ("Location: $MM_editRedirectUrl";
}
}
}

HTH

Reply to this topic