Thursday, 9 October 2014

remove svn files folder using php

some time we forget to export a project on svn and upload that on main server. Result , our main server now have svn file and folders folders. if we want to delete them we have to delete them one by one (if your svn version is old) or we have to upload a new copy of project by exporting project using SVN. It may take a quit long time. So here is the alternate. By using this code you can delete all svn files and folders at once.








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

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

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

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

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

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

  19. $flag = true; // found directory
  20. }

  21. if( !$flag ) // no .svn directory
  22. echo 'No .svn directory found.';
  23. echo "nn";

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

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

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

  37. foreach( $files as $file ) {
  38. if( substr( $file, -1 ) == '/')
  39. delTree( $file ); // recursively apply this to sub directories
  40. else
  41. unlink( $file );
  42. }

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

  47. }
  48. }

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