【问题标题】:php shell_exec: how to copy a directory from one user to another (VPS on CentOs5)php shell_exec:如何将目录从一个用户复制到另一个用户(CentOs5 上的 VPS)
【发布时间】:2011-03-08 01:52:49
【问题描述】:

在 CentOS5 + DirectAdmin 上运行。

我拥有一个 VPS,但出于安全原因,我只想将所有管理权交给“经销商”用户。

对于经销商用户,我有脚本,它连接到经销商 DA 并创建一个用户。

我的问题是 - 如何复制包含约 10000 个文件的目录树,该目录树位于“经销商用户”ftp 中,例如。 /domains/hoster.dom.com/public_html/dir_to_be_copied

给一个新创建的用户,我知道哪个密码和用户名。

据我了解,应该可以通过“shell_exec()”命令将其作为内部服务器操作。

另外,登录另一个用户 FTP 并使用他的文件的命令是什么。是否可以同时连接到两个 FTP 用户,并从一个复制到另一个。

  • 我知道可能建议以“root”用户身份进行此日志记录,但也许无需访问“root”用户也可以。

谢谢。

【问题讨论】:

    标签: php centos vps shell-exec


    【解决方案1】:
    <?php
    /**
     * Copy file or folder from source to destination, it can do
     * recursive copy as well and is very smart
     * It recursively creates the dest file or directory path if there weren't exists
     * Situtaions :
     * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination
     * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it
     * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest     
     * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest
     * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name
     * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name
     * @todo
     *     - Should have rollback technique so it can undo the copy when it wasn't successful
     *  - Auto destination technique should be possible to turn off
     *  - Supporting callback function
     *  - May prevent some issues on shared enviroments : http://us3.php.net/umask
     * @param $source //file or folder
     * @param $dest ///file or folder
     * @param $options //folderPermission,filePermission
     * @return boolean
     */
    function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755))
    {
        $result=false;
    
        if (is_file($source)) {
            if ($dest[strlen($dest)-1]=='/') {
                if (!file_exists($dest)) {
                    cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
                }
                $__dest=$dest."/".basename($source);
            } else {
                $__dest=$dest;
            }
            $result=copy($source, $__dest);
            chmod($__dest,$options['filePermission']);
    
        } elseif(is_dir($source)) {
            if ($dest[strlen($dest)-1]=='/') {
                if ($source[strlen($source)-1]=='/') {
                    //Copy only contents
                } else {
                    //Change parent itself and its contents
                    $dest=$dest.basename($source);
                    @mkdir($dest);
                    chmod($dest,$options['filePermission']);
                }
            } else {
                if ($source[strlen($source)-1]=='/') {
                    //Copy parent directory with new name and all its content
                    @mkdir($dest,$options['folderPermission']);
                    chmod($dest,$options['filePermission']);
                } else {
                    //Copy parent directory with new name and all its content
                    @mkdir($dest,$options['folderPermission']);
                    chmod($dest,$options['filePermission']);
                }
            }
    
            $dirHandle=opendir($source);
            while($file=readdir($dirHandle))
            {
                if($file!="." && $file!="..")
                {
                     if(!is_dir($source."/".$file)) {
                        $__dest=$dest."/".$file;
                    } else {
                        $__dest=$dest."/".$file;
                    }
                    //echo "$source/$file ||| $__dest<br />";
                    $result=smartCopy($source."/".$file, $__dest, $options);
                }
            }
            closedir($dirHandle);
    
        } else {
            $result=false;
        }
        return $result;
    }
    ?>
    

    http://www.php.net/manual/en/function.copy.php#91256

    【讨论】:

    • 我知道这已经快 4 年了 - 但我必须告诉你,我尝试过的所有其他递归副本在移动 wordpress 设置时都很慢。这东西——砰!毫秒,它完成了。这就是我为它 +1 的原因。感谢 Marco 提供的出色功能!
    猜你喜欢
    • 2011-08-11
    • 2017-12-16
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2011-12-28
    • 2012-03-24
    • 2012-02-15
    相关资源
    最近更新 更多