【问题标题】:remove all .svn files and folder using php使用 php 删除所有 .svn 文件和文件夹
【发布时间】:2014-01-06 09:16:52
【问题描述】:

我错误地将我的代码上传到带有 svn 文件和文件夹的服务器上。我无权访问 SSH,因此无法运行命令。 那么是否有任何 php 代码可以使用它从我的项目中删除所有 .svn 文件夹。

【问题讨论】:

  • 您要删除所有以 .svn 结尾的文件夹和文件吗?
  • d.pr/i/I10m d.pr/i/OLmr 请检查这个项目在旧svn上的droplr图像,它在每个文件夹中创建.svn文件夹,所以我想删除所有.svn文件夹和其中的所有文件.
  • 只有一个文件夹吗?还是很多?
  • 我正在使用 joomla ,所以它会在 joomla 的每个文件夹中创建 .svn 文件夹
  • 1.获取所有目录(除了 .svn )的列表; 2、循环遍历所有的,删除.svn子目录;问题出在哪里?

标签: php svn


【解决方案1】:

编辑:

查找所有.svn 文件夹是一个复杂的过程,您需要使用递归函数。

代码链接:http://pastebin.com/i5QMGm1C

或在这里查看:

function rrmdir($dir)
{
    foreach(glob($dir . '/*') as $path) {
        if(is_dir($path)){
            rrmdir($path);
        }
        else{
            unlink($path);
        }
    }

    foreach(glob($dir . '/.*') as $path) {
        if(is_dir($path)){
            $base_name = basename($path);
            if ($base_name != '..' && $base_name != '.'){
                rrmdir($path);
            }
        }
        else{
            unlink($path);
        }
    }
    rmdir($dir);
}

function delete_dir($base, $dir)
{
    static $count = 0;
    foreach (glob($base . '/*') as $path){
        if(is_dir($path)){
            delete_dir($path, $dir);
        }
    }

    foreach (glob($base . '/.*') as $path){
        if(is_dir($path)){
            $base_name = basename($path);
            if ($base_name != '..' && $base_name != '.'){
                if ($base_name == $dir){
                    rrmdir($path);
                    echo 'Directory (' . $path . ') Removed!<br />';
                    $count++;
                }
                else {
                    delete_dir($path, $dir);
                }
            }
        }
    }
    return $count;
}

$base = $_SERVER['DOCUMENT_ROOT'];
$dir = '.svn';
$count = delete_dir($base, $dir);

echo 'Total: ' . $count . ' Folders Removed!';

【讨论】:

  • 不工作 警告:delete_dir() 缺少参数 2,在第 90 行的 D:\xampp\htdocs\project\trunk\solution\checkdir.php 中调用并在 D:\xampp\htdocs 中定义\project\trunk\solution\checkdir.php 在第 83 行
  • 我必须提供相对路径还是绝对路径
  • 我在lateralcode.com/remove-svn-php 上找到了一个代码示例,它列出了所有文件,但由于权限错误而无法删除文件
  • 您可以提供相对或绝对,但请确保不要添加最后一个斜杠,例如 /a/b/c 而不是 /a/b/c/
  • 我正在使用 $base = $_SERVER['DOCUMENT_ROOT'].'/project/trunk/solution/';但不工作..顺便说一句,我得到了解决方案..感谢您的支持.. :)
【解决方案2】:

我在这里得到了解决方案

复制自lateralcode,稍作修改

$path = $_SERVER['DOCUMENT_ROOT'].'/work/remove-svn-php/'; // path of your directory 
header( 'Content-type: text/plain' ); // plain text for easy display

// preconditon: $dir ends with a forward slash (/) and is a valid directory
// postcondition: $dir and all it's sub-directories are recursively
// searched through for .svn directories. If a .svn directory is found,
// it is deleted to remove any security holes. 
function removeSVN( $dir ) {
    //echo "Searching: $dir\n\t";

    $flag = false; // haven't found .svn directory
    $svn = $dir . '.svn';

    if( is_dir( $svn ) ) {
        if( !chmod( $svn, 0777 ) )
            echo "File permissions could not be changed (this may or may not be a problem--check the statement below).\n\t"; // if the permissions were already 777, this is not a problem

        delTree( $svn ); // remove the .svn directory with a helper function

        if( is_dir( $svn ) ) // deleting failed
            echo "Failed to delete $svn due to file permissions.";
        else
            echo "Successfully deleted $svn from the file system.";

        $flag = true; // found directory
    }

    if( !$flag ) // no .svn directory
        echo 'No .svn directory found.';
    echo "\n\n";

    $handle = opendir( $dir );
    while( false !== ( $file = readdir( $handle ) ) ) {
        if( $file == '.' || $file == '..' ) // don't get lost by recursively going through the current or top directory
            continue;

        if( is_dir( $dir . $file ) )
            removeSVN( $dir . $file . '/' ); // apply the SVN removal for sub directories
    }
}

// precondition: $dir is a valid directory
// postcondition: $dir and all it's contents are removed
// simple function found at http://www.php.net/manual/en/function.rmdir.php#93836
function delTree( $dir ) {
    $files = glob( $dir . '*', GLOB_MARK ); // find all files in the directory

    foreach( $files as $file ) {
        if( substr( $file, -1 ) == '/')
            delTree( $file ); // recursively apply this to sub directories
        else
            unlink( $file );
    }

    if ( is_dir( $dir ) ){
                //echo $dir;
               // die;
        rmdir( $dir ); // remove the directory itself (rmdir only removes a directory once it is empty)

            }
      }

// remove all .svn directories in the 
// current directory and sub directories 
// (recursively applied)
removeSVN($path);

【讨论】:

    猜你喜欢
    • 2011-06-03
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多