Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Created May 6, 2014 15:18
Embed
What would you like to do?
Move files around in WP
<?php
function bbg_toolbox() {
if ( ! is_super_admin() ) {
return;
}
if ( empty( $_GET['bbg-toolbox'] ) ) {
return;
}
global $wpdb;
$prd = 614;
$stg = 860;
switch_to_blog( $prd );
$prd_dir = wp_upload_dir();
$prd_path = $prd_dir['basedir'];
restore_current_blog();
switch_to_blog( $stg );
// WP sucks
$stg_path = str_replace( $prd, $stg, $prd_path );
restore_current_blog();
echo '<pre>';
print_r( $prd_path );
print_r( "\n\r" );
print_r( $stg_path );
echo '</pre>';
bbg_recurse_copy( $stg_path, str_replace( $stg, '860-bu', $stg_path ) );
bbg_rrmdir( $stg_path );
bbg_recurse_copy( $prd_path, $stg_path );
}
add_action( 'admin_init', 'bbg_toolbox' );
function bbg_recurse_copy($src,$dst) {
$dir = opendir( $src );
wp_mkdir_p( $dst );
while ( false !== ( $file = readdir( $dir )) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
bbg_recurse_copy( $src . '/' . $file, $dst . '/' . $file );
} else {
copy( $src . '/' . $file, $dst . '/' . $file );
}
}
}
closedir( $dir );
}
function bbg_rrmdir( $dir ) {
foreach ( glob( $dir . '/*' ) as $file ) {
if ( is_dir( $file ) ) {
rrmdir( $file );
} else {
unlink( $file );
}
}
rmdir( $dir );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment