Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从服务器添加文件wordpress插件检查并限制999个文件_Php_Wordpress - Fatal编程技术网

Php 从服务器添加文件wordpress插件检查并限制999个文件

Php 从服务器添加文件wordpress插件检查并限制999个文件,php,wordpress,Php,Wordpress,我使用它是为了从服务器添加文件,我有两个问题。没有跳过文件,因此我需要自己修改代码以添加检查过程,但问题是每个文件的检查过程都非常慢。第二个问题是插件不能一次添加超过999个文件,我需要向媒体库添加大约50000个文件 function thisismyurl_add_media_to_library() { global $wpdb; $file_count = 0; /* if the user isn't an admin user, don't do any

我使用它是为了从服务器添加文件,我有两个问题。没有跳过文件,因此我需要自己修改代码以添加检查过程,但问题是每个文件的检查过程都非常慢。第二个问题是插件不能一次添加超过999个文件,我需要向媒体库添加大约50000个文件

function thisismyurl_add_media_to_library() {

    global $wpdb;

    $file_count = 0;

    /* if the user isn't an admin user, don't do anything */
    if ( ! current_user_can( 'manage_options' ) )
        return;




    /* (you'll want to reset this to your path */   
    $file_path = ABSPATH . '/import/path/to/files/';

    /* get a list of all files in a specific directory */
    $files = glob( $file_path . '*.jpg');


    if ( ! empty( $files ) ) {

        /* now we loop the files */
        foreach ( $files as $file ) {
            unset( $post_id );


            /* it's likely that a server will time out with too many files so we're going to limit it to 999 new files */
            if ( $file_count < 999 ) {


                $filename = str_replace( $file_path, '', $file );
                /* check to see if the image already exists */
                $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM  $wpdb->posts WHERE post_title = %s", $filename ) );


                /* the file does not exist */
                if ( empty( $post_id ) ) {

                    /* only count new files when checking for the file count */
                    $file_count++;

                    $attachment = array(
                        'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                        'post_mime_type' => wp_check_filetype( basename( $file ), null ),
                        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                        'post_content'   => '',
                        'post_status'    => 'inherit'
                    );

                    wp_insert_attachment( $attachment, $filename );


                    /* this is commented out for now, but if you uncomment it, the code will delete each file after it's been inserted */
                    /*

                    if ( $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM  $wpdb->posts WHERE post_title = %s", $filename ) ) )
                        unlink( $file );

                    */


                } /* if */
            }

        } /* foreach  */

    } /* if */

}
add_action( 'wp_head', 'thisismyurl_add_media_to_library' );
我更改代码以检查文件是否在媒体库中并跳过它:

class.add-from-server.php

function handle_imports() { 
    if ( !empty($_POST['files']) && !empty($_POST['cwd']) ) {
        $query_images_args = array(
        'post_name' => trim ( $post_name ), 'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
        );

        $query_images = new WP_Query( $query_images_args );
        $images = array();
        foreach ( $query_images->posts as $image) {
            $image_trim = wp_get_attachment_url( $image->ID );
            $image_trim = explode('/', $image_trim);
            $images[] = end($image_trim);
        }
    // $images is the array with the filenames where I stock the media library files
        $files = array_map('stripslashes', $_POST['files']);
        $cwd = trailingslashit(stripslashes($_POST['cwd']));
        $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
        $import_date = isset($_REQUEST['import-date']) ? $_REQUEST['import-date'] : 'file';

        $import_to_gallery = isset($_POST['gallery']) && 'on' == $_POST['gallery'];
        if ( ! $import_to_gallery && !isset($_REQUEST['cwd']) )
        $import_to_gallery = true; // cwd should always be set, if it's not, and neither is gallery, this must be the first page load.

        if ( ! $import_to_gallery )
        $post_id = 0;

        flush();
        wp_ob_end_flush_all();

        foreach ( (array)$files as $file ) {
            if (!in_array($file, $images)) {
             // here I ask if the image that I want to add is in the media library or not
                $filename = $cwd . $file;
                $id = $this->handle_import_file($filename, $post_id, $import_date);
                if ( is_wp_error($id) ) {
                    echo '<div class="updated error"><p>' . sprintf(__('<em>%s</em> was <strong>not</strong> imported due to an error: %s', 'add-from-server'), esc_html($file), $id->get_error_message() ) . '</p></div>';
                } else {
                    //increment the gallery count
                    if ( $import_to_gallery )
                    echo "<script type='text/javascript'>jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);</script>";
                    echo '<div class="updated"><p>' . sprintf(__('<em>%s</em> has been added to Media library', 'add-from-server'), esc_html($file)) . '</p></div>';
                }
                flush();
                wp_ob_end_flush_all();
            } else {
                echo '<div class="updated error">File '.$file.' had been skipped because it is already in the media library.</div>';
            }
        }
    }
}

第二个问题是999文件限制,如何克服这个限制?我相信它与wordpress代码有关,但不知道如何绕过它。

好吧,我不会直接回答你的问题,因为我不明白你为什么要使用插件来实现这一点,但是。。。如果不使用插件,您尝试做的事情非常简单

首先,您需要循环一个目录,然后检查媒体是否存在,如果不存在,请将媒体添加到媒体库中

function thisismyurl_add_media_to_library() {

    global $wpdb;

    $file_count = 0;

    /* if the user isn't an admin user, don't do anything */
    if ( ! current_user_can( 'manage_options' ) )
        return;




    /* (you'll want to reset this to your path */   
    $file_path = ABSPATH . '/import/path/to/files/';

    /* get a list of all files in a specific directory */
    $files = glob( $file_path . '*.jpg');


    if ( ! empty( $files ) ) {

        /* now we loop the files */
        foreach ( $files as $file ) {
            unset( $post_id );


            /* it's likely that a server will time out with too many files so we're going to limit it to 999 new files */
            if ( $file_count < 999 ) {


                $filename = str_replace( $file_path, '', $file );
                /* check to see if the image already exists */
                $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM  $wpdb->posts WHERE post_title = %s", $filename ) );


                /* the file does not exist */
                if ( empty( $post_id ) ) {

                    /* only count new files when checking for the file count */
                    $file_count++;

                    $attachment = array(
                        'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                        'post_mime_type' => wp_check_filetype( basename( $file ), null ),
                        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                        'post_content'   => '',
                        'post_status'    => 'inherit'
                    );

                    wp_insert_attachment( $attachment, $filename );


                    /* this is commented out for now, but if you uncomment it, the code will delete each file after it's been inserted */
                    /*

                    if ( $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM  $wpdb->posts WHERE post_title = %s", $filename ) ) )
                        unlink( $file );

                    */


                } /* if */
            }

        } /* foreach  */

    } /* if */

}
add_action( 'wp_head', 'thisismyurl_add_media_to_library' );