Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Folder

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I am writing some code to create two folders automatically but it seems to be giving the incorrect ownership (root rather than user) and wrong permissions - they need to be readable and writable. This is the first I've done this sort of thing so I hope someone can help!

Code:
$oldumask = umask(0); 
$ServerPath = getenv("DOCUMENT_ROOT");

$NewUserFolder = mysql_insert_id() . "-" . CCGetParam("FirstName", "") . CCGetParam("LastName", "");
mkdir($ServerPath . "/files/" .  $NewUserFolder, 01755);
mkdir($ServerPath . "/files/" .  $NewUserFolder . "/temp/", 01755);
umask($oldumask);

They are being creating with the proper name and in the proper place though.

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 

is the script running with root privileges or with user privileges? if it is running with user privileges i wouldn't set the sticky bit. just go for 0755 (otherwise only that user can unstick it later)

if you create with root privileges then can you change the owner after creation using
chown($ServerPath . "/files/" . $NewUserFolder, $uid).


 
Do you mean
Code:
mkdir($ServerPath . "/files/" .  $NewUserFolder, [b]0755[/b]);
mkdir($ServerPath . "/files/" .  $NewUserFolder . "/temp/", [b]0755[/b]);
Also note that the permission of the folde created also depends on the value of umask on your server.
So the result permissions are permission applied using mkdir() - value of umask.

Hope that will help you.


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I realized after I posted that I shouldn't be running sticky bit but I wasn't sure about umask. With both removed, it appears that the ownership is being set to root. php.net doesn't seem to clear it up for me. How can I be sure that the ownership is not set to root?

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
I forgot to say that for some reason, chown() will not run and gives an "Operation not permitted" error.

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
chown does not work on remote files. are the directories on the web server? i assume so from the code.

is php's safe mode turned on? try turning it off and seeing whether it works (safe mode checks the uids before performing the file i/o action there is a known 'bug' around permissions and safe mode -
 
Yes, the script and folders are in the same place. Thanks for the tip on safe mode as I haven't tried that yet.

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
The safe mode bug documentation does indeed describe this very problem although in this case, it is not related to using the system's temp folder (I am using one of my own creation). But I could find nothing to indicate how or even if the safe mode can be disabled, then reenabled again, in PHP. Does anyone know?

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
edit the php.ini file and look for a line saying "Safe_mode = On" and turn it off!

you might try playing around with the other safe_mode parameters instead of just turning it off but it's as well to try with it turned off first - to see whether that's the problem.

 
Well, I do not have access to php.Ini as most people don't. I will see if my hosting company is willing but I doubt it.

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
the penny drops. since you had other users i assumed that you were root.

upload a file with a php to your website with the code
Code:
<? phpinfo(); ?>

call the file from a browser and let me know whether safe mode is turned on.

i'll have another think about potential other methods: how about a php access control system, for example?
 
I do own my own account where I can add other accounts, create domains and even resell, but it is done through a provider. Since my last posting, I wrote to the provider and got a quick response as follows:

We run php by default with safe mode off.

The reason this is occuring, is because php is running as an apache module. So the apache user is issueing the mkdir system command, resulting in ownership of the user 'nobody' on the folders that are created.

I am looking into the mkdir function and will get back to you.


Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
good provider for getting back so quickly! let us know what the outcome is.

cheers
Justin
 
Yes, I am quite happy so far, though I signed on with them only this past Monday. They have been extraordinarily helpful with fast personal responces.

That said, they have since provided a tip that I have not had time to try but it sounds valid, and that is to use PHP's FTP API to create the folder.

The following apparently is from the php.net site, although I am not sure:

Code:
<?php
// create directory through FTP connection
function FtpMkdir($path, $newDir) {
  
       $server='ftp.yourserver.com'; // ftp server
       $connection = ftp_connect($server); // connection
  
 
       // login to ftp server
       $user = "me";
       $pass = "password";
       $result = ftp_login($connection, $user, $pass);

   // check if connection was made
     if ((!$connection) || (!$result)) {
       return false;
       exit();
       } else {
         ftp_chdir($connection, $path); // go to destination dir
       if(ftp_mkdir($connection,$newDir)) { // create directory
           return $newDir;
       } else {
           return false;       
       }
   ftp_close($conn_id); // close connection
   }

}
?>

Once I test it, I'll post the results.

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
looks fine although a bit of a kludge (creating an ftp connection...)!

you would, i guess, want to add further parameters to the function call for the server, username and password etc.
 
As long as it works, it will do as it might me the olny workaround for me. I played with it briefly yesterday before leaving town for the weekend but it appears to be trying to open the folder before it is created. It gives only a few errors rather than creating the folders. I'll need to look more closely but if anyone has ideas, I'm happy to hear them!

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
do you set the user up first by creating his home directory and ftp account?
 
Ok, I have the function working and creating the folders just as it should. I also added input for ftp_chmod() but it seems to be doing nothing, though there are no errors either. Maybe someone can see something wrong:

Code:
// create directory through FTP connection
function FtpMkdir($path, $newDir, $permissions) {
       $server='ftp.pc-homepage.com'; // ftp server
       $connection = ftp_connect($server); // connection
  
// login to ftp server
       $user = "username";
       $pass = "password";
       $result = ftp_login($connection, $user, $pass);

// check if connection was made
   if ((!$connection) || (!$result)) {
       return false;
       exit();
    } else {
       ftp_chdir($connection, $path); // go to destination dir
    if(ftp_mkdir($connection, $newDir)) { // create directory
      return $newDir;
		ftp_chmod($connection, $permissions, $path . $newDir);
       } else {
           return false;       
       }
   ftp_close($conn_id); // close connection
   }
}

I am calling the function like this:

Code:
FtpMkdir($FTPPath . "/files/", $NewUserFolder, "0777");
FtpMkdir($FTPPath . "/files/" . $NewUserFolder, "temp", "0777");

Any ideas about ftp_chmod?

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Ok, I have the function working and creating the folders just as it should. I also added input for ftp_chmod() but it seems to be doing nothing, though there are no errors either. Maybe someone can see something wrong:

Code:
// create directory through FTP connection
function FtpMkdir($path, $newDir, $permissions) {
       $server='ftp.servername.com'; // ftp server
       $connection = ftp_connect($server); // connection
  
// login to ftp server
       $user = "username";
       $pass = "password";
       $result = ftp_login($connection, $user, $pass);

// check if connection was made
   if ((!$connection) || (!$result)) {
       return false;
       exit();
    } else {
       ftp_chdir($connection, $path); // go to destination dir
    if(ftp_mkdir($connection, $newDir)) { // create directory
      return $newDir;
		ftp_chmod($connection, $permissions, $path . $newDir);
       } else {
           return false;       
       }
   ftp_close($conn_id); // close connection
   }
}

I am calling the function like this:

Code:
FtpMkdir($FTPPath . "/files/", $NewUserFolder, "0777");
FtpMkdir($FTPPath . "/files/" . $NewUserFolder, "temp", "0777");

Any ideas about ftp_chmod?

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top