BP_Attachment::upload( array $file, string $upload_dir_filter = '', string|null $time = null )
Upload the attachment.
Description
Parameters
- $file
-
(Required) The appropriate entry the from $_FILES superglobal.
- $upload_dir_filter
-
(Optional) A specific filter to be applied to 'upload_dir' (optional).
Default value: ''
- $time
-
(Optional) Time formatted in 'yyyy/mm'.
Default value: null
Return
(array) On success, returns an associative array of file attributes. On failure, returns an array containing the error message (eg: array( 'error' => $message ) )
Source
File: bp-core/classes/class-bp-attachment.php
public function upload( $file, $upload_dir_filter = '', $time = null ) { /** * Upload action and the file input name are required parameters. * * @see BP_Attachment:__construct() */ if ( empty( $this->action ) || empty( $this->file_input ) ) { return false; } /** * Add custom rules before enabling the file upload */ add_filter( "{$this->action}_prefilter", array( $this, 'validate_upload' ), 10, 1 ); // Set Default overrides. $overrides = array( 'action' => $this->action, 'upload_error_strings' => $this->upload_error_strings, ); /** * Add a mime override if needed * Used to restrict uploads by extensions */ if ( ! empty( $this->allowed_mime_types ) ) { $mime_types = $this->validate_mime_types(); if ( ! empty( $mime_types ) ) { $overrides['mimes'] = $mime_types; } } /** * If you need to add some overrides we haven't thought of. * * @param array $overrides The wp_handle_upload overrides */ $overrides = apply_filters( 'bp_attachment_upload_overrides', $overrides ); $this->includes(); /** * If the $base_dir was set when constructing the class, * and no specific filter has been requested, use a default * filter to create the specific $base dir * @see BP_Attachment->upload_dir_filter() */ if ( empty( $upload_dir_filter ) && ! empty( $this->base_dir ) ) { $upload_dir_filter = array( $this, 'upload_dir_filter' ); } // Make sure the file will be uploaded in the attachment directory. if ( ! empty( $upload_dir_filter ) ) { add_filter( 'upload_dir', $upload_dir_filter, 10, $this->upload_dir_filter_args ); } // Helper for utf-8 filenames. add_filter( 'sanitize_file_name', array( $this, 'sanitize_utf8_filename' ) ); // Upload the attachment. $this->attachment = wp_handle_upload( $file[ $this->file_input ], $overrides, $time ); remove_filter( 'sanitize_file_name', array( $this, 'sanitize_utf8_filename' ) ); // Restore WordPress Uploads data. if ( ! empty( $upload_dir_filter ) ) { remove_filter( 'upload_dir', $upload_dir_filter, 10 ); } // Finally return the uploaded file or the error. return $this->attachment; }
Changelog
Version | Description |
---|---|
BuddyPress 2.3.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.