简体   繁体   中英

remove all .svn files and folder using php

by mistaken I upload my code on server with svn files and folders. I don't have access for SSH so can't run commands. so is there any php code by using which I can delete all .svn folders from my projects.

EDIT:

Finding all the .svn folders is a complicated process to do that you need a to use recursive functions.

Link to code: http://pastebin.com/i5QMGm1C

Or view it here:

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!';

I get a solution here it is

copied from lateralcode with little modification

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM