Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Copy a folder in a server folder on plugin activation Reply To: Copy a folder in a server folder on plugin activation

#237652
Ayanize
Participant

Hi,

Update

I found a workaround for this. Instead of uploading the folder from the plugin source to root, I zipped the source folder into a zip and uploaded that. Later I run a function to unzip that in the destination. Let me know if that’s okay.

/*....function to copy the zip file...*/

function recurse_copy($src,$dst = 0) {
    $src = plugin_dir_path( __FILE__ ) . 'api';
    $dst = $_SERVER['DOCUMENT_ROOT'] . '/api';
    $dir = opendir($src);
    @mkdir($dst);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src . '/' . $file) ) {
                recurse_copy($src . '/' . $file,$dst . '/' . $file);
            }
            else {
                copy($src . '/' . $file,$dst . '/' . $file);
            }
        }
    }
    closedir($dir);
}


add_action('plugins_loaded', 'recurse_copy', 10, 2);
function unzip_api(){

WP_Filesystem();
$destination = $_SERVER['DOCUMENT_ROOT'] . '/api';
$destination_path = $_SERVER['DOCUMENT_ROOT'] . '/api';
$unzipfile = unzip_file( $destination_path.'/api.zip', $destination_path);

   if ( $unzipfile ) {
      echo 'Successfully unzipped the file!';       
   } else {
      echo 'There was an error unzipping the file.';       
   }

}
add_action('admin_init', 'unzip_api');