title: Add file from server wordpress plugin checking and limit of 999 files
body: <p>I am using this <a href="https://wordpress.org/plugins/add-from-server/" rel="nofollow">wordpress plugin</a> in order to add files from server and I have 2 issues. Files are not being skipped, therefore I needed to alter the code myself in order to add a checking process, but the problem is that the checking process is very slow for each file. Second issue is that the plugin can't add more than 999 files at once and I need to add about 50000 files to the media library.</p>
  
  <p>Code that I altered to check if the file is in the media library and skip it:</p>
  
  <p>class.add-from-server.php</p>
  
  <pre><code>function handle_imports() { 
      if ( !empty($_POST['files']) &amp;&amp; !empty($_POST['cwd']) ) {
          $query_images_args = array(
          'post_name' =&gt; trim ( $post_name ), 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt;'image', 'post_status' =&gt; 'inherit', 'posts_per_page' =&gt; -1,
          );
  
          $query_images = new WP_Query( $query_images_args );
          $images = array();
          foreach ( $query_images-&gt;posts as $image) {
              $image_trim = wp_get_attachment_url( $image-&gt;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']) &amp;&amp; 'on' == $_POST['gallery'];
          if ( ! $import_to_gallery &amp;&amp; !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-&gt;handle_import_file($filename, $post_id, $import_date);
                  if ( is_wp_error($id) ) {
                      echo '&lt;div class="updated error"&gt;&lt;p&gt;' . sprintf(__('&lt;em&gt;%s&lt;/em&gt; was &lt;strong&gt;not&lt;/strong&gt; imported due to an error: %s', 'add-from-server'), esc_html($file), $id-&gt;get_error_message() ) . '&lt;/p&gt;&lt;/div&gt;';
                  } else {
                      //increment the gallery count
                      if ( $import_to_gallery )
                      echo "&lt;script type='text/javascript'&gt;jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);&lt;/script&gt;";
                      echo '&lt;div class="updated"&gt;&lt;p&gt;' . sprintf(__('&lt;em&gt;%s&lt;/em&gt; has been added to Media library', 'add-from-server'), esc_html($file)) . '&lt;/p&gt;&lt;/div&gt;';
                  }
                  flush();
                  wp_ob_end_flush_all();
              } else {
                  echo '&lt;div class="updated error"&gt;File '.$file.' had been skipped because it is already in the media library.&lt;/div&gt;';
              }
          }
      }
  }
  </code></pre>
  
  <p>So please help 
  1. How can I speed up the checking process, want to mention that this code is the one that slowing down the process (true that I have 10000 images in the media library):</p>
  
  <pre><code>$query_images_args = array(
  'post_name' =&gt; trim ( $post_name ), 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt;'image', 'post_status' =&gt; 'inherit', 'posts_per_page' =&gt; -1,
  );
  
  $query_images = new WP_Query( $query_images_args );
  $images = array();
  foreach ( $query_images-&gt;posts as $image) {
      $image_trim = wp_get_attachment_url( $image-&gt;ID );
      $image_trim = explode('/', $image_trim);
      $images[] = end($image_trim);
  }
  </code></pre>
  
  <p>Second issue the one with the 999 files limit, how to overcome this limit? I believe is related to the wordpress code but don't know how to by pass it.</p>