duannai5858 2011-12-14 13:16
浏览 52
已采纳

ftp_site()不想更改文件夹上的CHMOD

I have a joomla page on which I want to install a few extensions. However due to the chmod permissions I am unable to upload and install the packages. Following the guide here I can see that in order for the plugins to be installed there are too many folders which permissions have to be changed.

For that I am creating a script which will iterate through each of the required folders and will change the permissions from 0775 to 0777.

<?php
// SET THE DESIRED CHMOD VALUE
if ($_GET['chmod']) { 
        $ftp_chmod = $_GET['chmod']; 
    } 
    else { 
        $ftp_chmod = "0755"; 
    }
echo "chmod=" . $ftp_chmod . '<br />';
echo getcwd() . '<br />';
$currdir = getcwd(); // get current directory

// ESTABLISH AN FTP LOGIN SESSION
$ftp_server='example.com';
$ftp_user='username';
$ftp_pass='*****';
$conn_id = ftp_connect("$ftp_server");

if ( ftp_login($conn_id, $ftp_user, $ftp_pass) ) {
    echo 'FTP CONNECTION IS SUCCESSFULL <br />';
}
else {
    echo 'BAD CREDENTIALS';
    exit();
}

// Define the folders for which the CHMODE will change the values
// There must be a leading space in front of the path in order for CHMOD to work
$folder_path = array(
    ' ' . $currdir . '/modules/',
    ' ' . $currdir . '/plugins/'
    ' ' . $currdir . '/tmp/',
    ' ' . $currdir . '/cache/'
);

echo '<br />';
foreach ( $folder_path as $key => $value )
{
    $path = trim($value); // The leading space must be trimed fo is_dir() function to work

    if ( is_dir($path) == true ) {

        echo $path . ' -- ' . '<span style="color: #00B200">OK</span><br />';
        echo 'CHMOD ' . $ftp_chmod . '  ' . $value . '<br />';

        if (ftp_site($conn_id, 'CHMOD ' . $ftp_chmod . $value)) {
            echo 'CHMOD ' . $ftp_chmod . ' IS <span style="color: #00B200">SUCCESSFULL</span><br /><br />';
        }
        else {
            echo '<span style="color: crimson">CHMOD FAILED!</span><br /><br />';
        }
    }
    else {
        echo $path . ' -- ' . '<span style="color: crimson"><b>NOT EXIST</b></span><br />';
    } // end if ( is_dir($path) == true ) else

} // end foreach ( $folder_path as $key => $value )

ftp_close($conn_id);
?>

Please note that the actual script is much larger due to the many folders that need to be changed. The folders shown in $folder_path = array() are just an example

When I execute the script on my server i get the folowind output:

chmod=0777
/var/www/example/data/www/example.com
FTP CONNECTION IS SUCCESSFULL 

/var/www/example/data/www/example.com/modules/ -- OK
CHMOD 0777 /var/www/example/data/www/example.com/modules/
CHMOD FAILED!

/var/www/example/data/www/example.com/plugins/ -- OK
CHMOD 0777 /var/www/example/data/www/example.com/plugins/
CHMOD FAILED!

/var/www/example/data/www/example.com/tmp/ -- OK
CHMOD 0777 /var/www/example/data/www/example.com/tmp/
CHMOD FAILED!

/var/www/example/data/www/example.com/cache/ -- OK
CHMOD 0777 /var/www/example/data/www/example.com/cache/
CHMOD FAILED!

DOES ANYONE HAVE ANY IDEA ON HOW TO CHANGE THE CHMOD VALUE ON SO MANY FOLDERS?


UPDATE:
I also have to mention that I have tried to change the CHMOD value on each folder individually via FTP client and it is successfull. The problem comes when i have to change them through the script. The same acc with root privilege access is used from the FTP client and the script to change the files!

  • 写回答

2条回答 默认 最新

  • dpvv37755 2011-12-15 14:30
    关注

    I have found a solution to the problem. What I did was a small modification to the foreach part of the script which made it work. Basically I just subtracted the required number of characters from the path returned from the getcwd() and that solved my problem.

    Here is the change that i did:

    ---- REST OF THE SCRIPT OMITED -----
    
    foreach ( $folder_path as $key => $value )
    {
        $path = trim($value); // The leading space must be trimed fo is_dis() function to work
    
        if ( is_dir($path) == true ) {
    
            $value_short = substr($value, 19); // <------------- Subtruct the required number od chars in order to create a relative path
    
            echo $path . ' -- ' . '<span style="color: #00B200">OK</span><br />';
            echo 'CHMOD ' . $ftp_chmod . '  ' . $value_short . '<br />';
    
    
            if (ftp_site($conn_id, "CHMOD $ftp_chmod $value_short")) { // <-------------- Use the relative path in the function
                echo 'CHMOD ' . $ftp_chmod . ' IS <span style="color: #00B200">SUCCESSFULL</span><br /><br />';
            }
            else {
                echo '<span style="color: crimson">CHMOD FAILED!</span><br /><br />';
            }
        }
        else {
            echo $path . ' -- ' . '<span style="color: crimson"><b>NOT EXIST</b></span><br />';
        } // end if ( is_dir($path) == true ) else
    
    } // end foreach ( $folder_path as $key => $value )
    
    ---- REST OF THE SCRIPT OMITED -----
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab