Forums

Get help. Give help. A Web Design and Development Community.

Copy a folder in a server folder on plugin activation

  • # February 1, 2016 at 1:18 am

    Hi,

    I am writing a plugin which comes with some sub-folders. One of them is ‘content’ folder.

    Now I am writing this function

    function create_content_dir() {
    
        $upload = wp_upload_dir();
        $upload_dir = $_SERVER['DOCUMENT_ROOT'];
        $upload_dir = $upload_dir . '/content';
        if (! is_dir($upload_dir)) {
           mkdir( $upload_dir, 0700 );
        }
    }
    
    register_activation_hook( __FILE__, 'create_content_dir' );
    

    The function creates a folder named content in the server root successfully when the plugin is activated. However, I will that all the content from my plugin folders are also copied to the server folder as is.

    How I can achieve this?

    # February 3, 2016 at 6:00 am

    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.'https://cdn.css-tricks.com/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');
    
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We have a pretty good* newsletter.