Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Server Side Programming > PHP Programming
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 03-06-2005, 05:15 PM
  #1
JaeSun
Lord (Level 16)
 
Join Date: Apr 2002
Posts: 728
iTrader: (0)
JaeSun is on a distinguished road
mkdir, php ftp api, and chmodding ...

ok, i was needing to know how to make directories ...

so found the php mkdir function...

but within the comments at:

http://us2.php.net/mkdir

he states:

Quote:
If you're on a shared *nix server, a directory created through mkdir() will not be assigned to you, but to the user that your host's server or php process is running under, usually 'nobody', 'apache' or 'httpd'.

In practice, this means that you can create directories, even add files to them, but you can't delete the directory or its contents nor change permissions.
so the person who made the comment said to use PHP's FTP API ..

which is:

PHP 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
   
}

}
?>
got that to work ... i can create directories as I please ....

but now, it creates directories I think to 755 (drwxr-xr-x)

but I need to be able to upload to those directories that I created with my php script ..

and im running into a bit of a snag ....

i am trying to use php's chmod function

PHP Code:
chmod("/home/whiteazn/public_html/test/New_Directory_Created_by_script"0777); 
but it gives me this error:

Quote:
Operation not permitted in /home/whiteazn/public_html/fbc/test/createdir.php on line 131
i find this (2nd post from the top):

http://www.christian-web-masters.com...=0&#entry50843

it says:

Quote:
I often get this error when PHP is running under nobody and the directory you are trying to chmod is owned by someone else.

The way around it that I can see is to:

1) Ask the user to chmod it instead (most scripts do this)
2) Create an FTP connection in your script to your own server and log in with the account details and then chmod via FTP in the script.
3) Use a perl script and hope that runs under the owner instead
i tried number 2:

PHP Code:
function ftpmkdir($path$newDir
{

   
$server="ftp.whiteazn.com"// ftp server
   
$connection ftp_connect($server); // connection

   // login to ftp server
   
$user "whiteazn";
   
$pass "jjclrg00";
   
$result ftp_login($connection$user$pass);

// check if connection was made
    
if ((!$connection) || (!$result)) 
    {
        return 
"Could not make a FTP connection";
        exit();
    }
    else
    {
        if (!(
ftp_chdir($connection$path))) // go to destination dir
        
{
            return 
"Could not change the directory";
        }
        if(
ftp_mkdir($connection,$newDir)) // create directory
        
{
            
$cmoddir =  "/home/whiteazn/public_html/test/" $newDir;
            
chmod("$cmoddir"0777);
            return 
$newDir;
        }
        else
        {
            return 
"cant create directory";       
        }
        
ftp_close($connection); // close connection
    
}


and it still gives me the same error ...

so how can i chmod in php ??
__________________
| www.whiteAzn.com |
JaeSun is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-06-2005, 10:06 PM
  #2
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,187
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
try this

PHP Code:
$oldmask umask(0);
         
mkdir ($path0777);
         
umask($oldmask); 
without the ftp.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-06-2005, 10:09 PM
  #3
JaeSun
Lord (Level 16)
 
Join Date: Apr 2002
Posts: 728
iTrader: (0)
JaeSun is on a distinguished road
err, i fixed it ... i forgot to update the thread

problem with using mkdir, is that if you use it, you cant chmod them unless you use a php script to chmod it (so if i were to go into a FTP client, i couldnt chmod it) ...

so i did this:

PHP Code:
$chmod_cmd "CHMOD 0777 " $newDir
            
$chmod ftp_site($connection$chmod_cmd); 
within the function ....
__________________
| www.whiteAzn.com |
JaeSun is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-07-2005, 09:38 AM
  #4
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,187
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
ok, my little script there lets you chmod it with mkdir. it takes away the stopping of you not being able to delete the folder after you make it.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards

Last edited by scoutt : 03-07-2005 at 09:48 AM.
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-07-2005, 10:40 AM
  #5
JaeSun
Lord (Level 16)
 
Join Date: Apr 2002
Posts: 728
iTrader: (0)
JaeSun is on a distinguished road
yea, i can chmod it, but then, read that in doing so, because the ownership was different (php and FTP), you have to create a script to delete files, etc as it wont let you in a FTP client ..
__________________
| www.whiteAzn.com |
JaeSun is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-07-2005, 11:28 AM
  #6
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,187
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
ok, you didn't pay attention. when you do this,
PHP Code:
$oldmask umask(0);
mkdir ($path0777);
umask($oldmask); 
it gives you ownership. you can delete using ftp

and if you want to delete in php you use unlink()
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-07-2005, 11:32 AM
  #7
JaeSun
Lord (Level 16)
 
Join Date: Apr 2002
Posts: 728
iTrader: (0)
JaeSun is on a distinguished road
actually, i did pay attention.

i DID that before, and whenever I go to FTP, it gives me wacko errors ...

to be more specific:

Quote:
"550 Could not change perms on testdir: Bad file descriptor
!Chmod failed. It may not be supported on remote site."
thats what happens when I try and chmod the directory created by:

PHP Code:
$oldmask umask(0);
mkdir ($path0777);
umask($oldmask); 
i really dont want to write a script just to delete files, chmod them ,etc when I need to ...

the chmod via FTP in php seems to work better .... no problems whatsoever
__________________
| www.whiteAzn.com |
JaeSun is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-07-2005, 11:37 AM
  #8
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,187
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
then it is not the script. the only error you should get is "permission denied", not "It may not be supported on remote site"
that could be your ftp and/or your host. NOT the script and not ownership. if it was ownership then you get permission denied. trust me, I have gotten it before.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-07-2005, 11:53 AM
  #9
JaeSun
Lord (Level 16)
 
Join Date: Apr 2002
Posts: 728
iTrader: (0)
JaeSun is on a distinguished road
could be the FTP...im using an old WS FTP LE ......

i was on the forums for my host, and other people got the same error message as me ...

so its prob the host ....

oh well, FTP chmodding/creating directories works just fine ....
__________________
| www.whiteAzn.com |
JaeSun is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-07-2005, 12:10 PM
  #10
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,187
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
yah, if it works fine with what you have then by all means, go for it. but what I gave you should work if it is not the host.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 05-15-2008, 08:51 AM
  #11
samwedge
Swordman (Level 9)
 
Join Date: May 2006
Posts: 85
iTrader: (0)
samwedge has disabled reputation
Hiya!

Sorry to drag up this really old thread, but just wanted to thank Scoutt for the bit of code. I've been searching around for this exact thing for ages!

Sam
samwedge is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Reply


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 04:11 PM.