最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp filesystem - How to Copy Upload Image using WP_Filesystem_Direct

programmeradmin6浏览0评论

I'm trying to export post_type and its attached image.

However, I'm stacking at copying file to export directory.

WP_Filesystem_Direct::copy failed to open stream ( No file or directory ).

Here the bellow is my code:

if ( isset( $_POST['export'] ) ) {

    if ( $_POST['export'] != 'export' ) return;

    $args = array(

        'post_type'         => 'notebook',

        'post_status'       => 'publish',

        'posts_per_page'    => -1,

    );

    $notebooks = new WP_Query( $args );

    if ( $notebooks->have_posts() ) {

        $time = date("Y-m-d-H-i-s");

        $wp_admin_path = ABSPATH . '/wp-admin';

        $export_path = ltc_get_upload_path() . 'export-notebooks';

        if( !class_exists( 'WP_Filesystem_Direct' ) ) {

            require $wp_admin_path . '/includes/class-wp-filesystem-base.php';
            require $wp_admin_path . '/includes/class-wp-filesystem-direct.php';

        }

        $wpsf = new WP_Filesystem_Direct( false );


        if ( !$wpsf->exists( $export_path ) ) {

            $wpsf->mkdir( $export_path  );

        }

        $outstream = fopen( get_temp_dir(  ) . "$time-exported-notebooks.csv", 'w');

        while( $notebooks->have_posts() ) {

            $notebooks->the_post();

            $post_id = get_the_ID();

            $attachment_id = get_post_thumbnail_id( $post_id );

            $image = get_attached_file( $attachment_id );

            $image_path = substr( $image, strlen( ltc_get_upload_path() ) );

            $destination = $export_path ."/$image_path";

            if ( $wpsf->is_file( $image ) ) { // return true

                $wpsf->copy( $image, $destination );

            }


        }


    }

}
发布评论

评论列表(0)

  1. 暂无评论