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

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #237561
    Ayanize
    Participant

    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?

    #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');
    
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.