www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] chmod problem

  1. #1
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304

    resolved [RESOLVED] chmod problem

    hi guys,

    Sorry to bother again but im running out of ideas and reading the php.net and this forums didnt help me that much...

    I want a function to chmod a bunch of files and folders that I regularly need to chmod (and very often forget about some of these...)

    So, im getting 'Operation not permitted' and 'file not found', although im sure the files/folders are there...

    Here comes my code, I know echoing everything is not the better way but it is just for debugging...

    PHP Code:
    //This array to include all the files and folders to chmod
    $ToChMod = array(
        
    'admin/',
        );
        

    function 
    MyChmod($item$mode='0777')
    {
    // This function gets an $item to be chmoded to $mode, if no $mode is specified then
    // the default is 0777
    // It checks if the $item is an array and changes all the values in it
    // This function excludes the . and .. resulting of the directory structure

    // Possible inputs: a Directory, a File or an Array. If its a directory or a file 
    // then it is handled just the same

    $output='';
    $path_parts pathinfo('chmod.php');
    $PathToHere getcwd();//'/home/mg46/public_html';//pathinfo($_SERVER['PHP_SELF']);
    echo $PathToHere.'<br><br>';
    if(!
    is_array($item))
        {
        
    chmod($PathToHere.$item,$mode);
        }
    //if is not array

    else 
        {
            
    // then it is an array
            
    foreach($item as $index
            {
                
    $changed=TRUE;
                if(
    $index != '.' && $index!='..')
                {
                    echo 
    $index.'<br>';
                    
    chmod($path_parts.$index,$mode) or ($changed=FALSE);
                }
                if(
    $changed==TRUE){$output.=$index.' changed to '.$mode.'<br>';}
                if(
    $changed==FALSE){$output.=$index.' was <b>not</b> changed <br>';}
            }
        }
    echo 
    $output;
    return 
    $output;    
    }
    //function

    if(chmod(__FILE__,0777)){echo 'Changed<br>';}
    else {echo 
    'Not changed<br>';}

    // __FILE__;//pathinfo($_SERVER['PHP_SELF']).'<br>';

    echo getcwd().' <<<<br><br><br>';

    MyChmod($ToChMod); 
    I cant even get to chmod the own running script...
    So whatever suggestion you may have it will be very welcome

    thank you
    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    Remember that your PHP script is normally run under a user ID that is specific to the web server, such as "nobody" or "www" or "apache"; it is not run under your personal user ID. Therefore, if the files are owned by you (your personal login account), only you (or the server's superuser) can chmod them - the webserver user will not be allowed to do so.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304
    yes I had forgot about it,
    but even when I tried changing the owner to my login information (the ftp username I logged in with when uploading files) it throws me a
    Warning: chown(): Operation not permitted in /home/myuser/public_html/chmod.php on line 37
    what I did for changing owner was:
    PHP Code:
    if($index != '.' && $index!='..')
                {
                    echo 
    $index.'<br>';
                    if(
    chown($index,'myuser')){echo "--Chown worked for:".$index;}
                    
    chmod($path_parts.$index,$mode) or ($changed=FALSE);
                } 
    so im stuck here too...

    I
    echo exec("whoami");
    and found out that the script is running as 'nobody',
    so I tried
    PHP Code:
    if(chown(__FILE__,'root')){echo "--Chown worked for: ".__FILE__;}
    echo 
    exec("whoami"); 
    but it didnt work -> Operation not permitted in /home/mg46/public_html/chmod.php on line 10

    So once again im stuck...
    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    I believe chown can only be done by the superuser.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304

    [FIXED] needed to ftp

    I got it, the thing was fixed when I ftp to the server,
    im using it now as a mass chmoder!

    You might improve it [especially the output... hehehe]
    PHP Code:
    function MyChmod($path$mod$ftp_user,$ftp_pass,$ftp_root,$ftp_serv)
    {
        
    /*
        $path must be an array and every parameter is mandatory
    */
      
       // set up basic connection
       
    $conn_id ftp_connect($ftp_serv);
      
       
    // login with username and password
       //$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
      
    if (@ftp_login($conn_id$ftp_user$ftp_pass)) {
       echo 
    "Connected as ".$ftp_user." @ ".$ftp_serv."\n<br>";
    } else {
       echo 
    "Couldn't connect as $ftp_user\n";
    }
       
       
    // try to chmod $path directory
       
    foreach ($path as $i){
       if (
    ftp_site($conn_id'CHMOD '.$mod.' '.$ftp_root.$i) !== false) {
           
    $success=TRUE;
           echo 
    '-Changed: '.$i.' to: '.$mod.'<br>';
       }
       else {
           
    $success=FALSE;
           echo 
    '** Failed to chamod: '.$i.'<br>';
       }}

       
    // close the connection
       
    ftp_close($conn_id);
       return 
    $success;

    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

  6. #6
    Join Date
    Jul 2005
    Location
    USA
    Posts
    912
    bump..nevermind
    Last edited by Webnerd; 05-15-2006 at 02:47 PM.
    Bitter web veteran

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles