如何在一个表单中使用相同的上传处理来区分两个上传?

时间:2014-07-29 20:42:28

标签: php wordpress file-upload plugins

我已经为我的插件创建了一个表单,它有两个上传字段;一个用于图像,一个用于zip文件。他们都使用相同的上传处理程序,我想将附件ID保存到数据库。问题是它们使用相同的上传处理程序,因此带有附件ID的变量的值将始终是最后一个上载字段。最好的方法是怎样做的?保存在数组中(第一个索引是第一个字段,第二个索引是第二个字段)?两个上传处理程序可能有点矫枉过正。任何想法如何以一种好的方式解决这个问题?

这是处理上传的功能:

function releases_action(){
    global $wpdb;

    // Upload cover
     $uploadfiles = $_FILES['uploadfiles'];

      if (is_array($uploadfiles)) {

        foreach ($uploadfiles['name'] as $key => $value) {

          // look only for uploded files
          if ($uploadfiles['error'][$key] == 0) {

            $filetmp = $uploadfiles['tmp_name'][$key];

            //clean filename and extract extension
            $filename = $uploadfiles['name'][$key];

            // get file info
            // @fixme: wp checks the file extension....
            $filetype = wp_check_filetype( basename( $filename ), null );
            $filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
            $filename = $filetitle . '.' . $filetype['ext'];
            $upload_dir = wp_upload_dir();

            /**
             * Check if the filename already exist in the directory and rename the
             * file if necessary
             */
            $i = 0;
            while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
              $filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
              $i++;
            }
            $filedest = $upload_dir['path'] . '/' . $filename;

            /**
             * Check write permissions
             */
            if ( !is_writeable( $upload_dir['path'] ) ) {
              $this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
              return;
            }

            /**
             * Save temporary file to uploads dir
             */
            if ( !@move_uploaded_file($filetmp, $filedest) ){
              $this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
              continue;
            }

            $attachment = array(
              'post_mime_type' => $filetype['type'],
              'post_title' => $filetitle,
              'post_content' => '',
              'post_status' => 'inherit'
            );

            $attach_id = wp_insert_attachment( $attachment, $filedest );
            require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
            wp_update_attachment_metadata( $attach_id,  $attach_data );
          }
        }

}

正如我所说,由于两个上传字段都使用相同的功能,因此$ attach_ID变量将是最新上传的值。

1 个答案:

答案 0 :(得分:1)

function releases_action(){
global $wpdb;

// Upload cover
 $uploadfiles = $_FILES['uploadfiles'];

  if (is_array($uploadfiles)) {

    foreach ($uploadfiles['name'] as $key => $value) {

      // look only for uploded files
      if ($uploadfiles['error'][$key] == 0) {

        $filetmp = $uploadfiles['tmp_name'][$key];

        //clean filename and extract extension
        $filename = $uploadfiles['name'][$key];

        // get file info
        // @fixme: wp checks the file extension....
        $filetype = wp_check_filetype( basename( $filename ), null );
        $filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
        $filename = $filetitle . '.' . $filetype['ext'];
        $upload_dir = wp_upload_dir();

        /**
         * Check if the filename already exist in the directory and rename the
         * file if necessary
         */
        $i = 0;
        while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
          $filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
          $i++;
        }
        $filedest = $upload_dir['path'] . '/' . $filename;

        /**
         * Check write permissions
         */
        if ( !is_writeable( $upload_dir['path'] ) ) {
          $this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
          return;
        }

        /**
         * Save temporary file to uploads dir
         */
        if ( !@move_uploaded_file($filetmp, $filedest) ){
          $this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
          continue;
        }

        $attachment = array(
          'post_mime_type' => $filetype['type'],
          'post_title' => $filetitle,
          'post_content' => '',
          'post_status' => 'inherit'
        );

        $attach_id = wp_insert_attachment( $attachment, $filedest );
        require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
        $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
        wp_update_attachment_metadata( $attach_id,  $attach_data );
        // $ids[]= $attach_id;
        // save $attach id here, its correct for this loop, on the next loop it will be different and so on..
      }
    }

return $ids; // or save here serialize() maybe needed depending on how you are saving. 
}