acf_upload_files()
This function will walk through the $_FILES data and upload each found.
No Hooks.
Returns
null. Nothing (null).
Usage
acf_upload_files( $ancestors );
- $ancestors(array)
- An internal parameter, not required.
Default:array()
Changelog
| Since 5.0.9 | Introduced. |
acf_upload_files() acf upload files code ACF 6.8.8
function acf_upload_files( $ancestors = array() ) {
if ( empty( $_FILES['acf'] ) ) {
return;
}
$file = acf_sanitize_files_array( $_FILES['acf'] ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified upstream.
// walk through ancestors.
if ( ! empty( $ancestors ) ) {
foreach ( $ancestors as $a ) {
foreach ( array_keys( $file ) as $k ) {
$file[ $k ] = $file[ $k ][ $a ];
}
}
}
// is array?
if ( is_array( $file['name'] ) ) {
foreach ( array_keys( $file['name'] ) as $k ) {
$_ancestors = array_merge( $ancestors, array( $k ) );
acf_upload_files( $_ancestors );
}
return;
}
// Bail early if file has error (no file uploaded).
if ( $file['error'] ) {
return;
}
$field_key = end( $ancestors );
$nonce_name = $field_key . '_file_nonce';
if ( empty( $_REQUEST['acf'][ $nonce_name ] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['acf'][ $nonce_name ] ), 'acf/file_uploader_nonce/' . $field_key ) ) {
return;
}
// Assign global _acfuploader for media validation.
$_POST['_acfuploader'] = $field_key;
// file found!
$attachment_id = acf_upload_file( $file );
// update $_POST
array_unshift( $ancestors, 'acf' );
acf_update_nested_array( $_POST, $ancestors, $attachment_id );
}