WP_REST_Attachments_Controller::upload_from_file
Handles an upload via multipart/form-data ($_FILES).
Метод класса: WP_REST_Attachments_Controller{}
Хуков нет.
Возвращает
array{ file: non-empty-string, url: non-empty-string, type: non-empty-string }|WP_Error. Data from wp_handle_upload().
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->upload_from_file( $files, $headers, $time );
- $files(массив) (обязательный)
- Data from the
$_FILESsuperglobal. - $headers(массив) (обязательный)
- HTTP headers from the request.
- $time(строка|null)
- Time formatted in
'yyyy/mm'.
По умолчанию:null
Список изменений
| С версии 4.7.0 | Введена. |
| С версии 6.6.0 | Added the $time parameter. |
Код WP_REST_Attachments_Controller::upload_from_file() WP REST Attachments Controller::upload from file WP 7.1.2
protected function upload_from_file( $files, $headers, $time = null ) {
if ( empty( $files ) ) {
return new WP_Error(
'rest_upload_no_data',
__( 'No data supplied.' ),
array( 'status' => 400 )
);
}
// Verify hash, if given.
if ( ! empty( $headers['content_md5'] ) ) {
$content_md5 = array_shift( $headers['content_md5'] );
$expected = trim( $content_md5 );
$actual = md5_file( $files['file']['tmp_name'] );
if ( $expected !== $actual ) {
return new WP_Error(
'rest_upload_hash_mismatch',
__( 'Content hash did not match expected.' ),
array( 'status' => 412 )
);
}
}
// Pass off to WP to handle the actual upload.
$overrides = array(
'test_form' => false,
);
// Bypasses is_uploaded_file() when running unit tests.
if ( defined( 'DIR_TESTDATA' ) && DIR_TESTDATA ) {
$overrides['action'] = 'wp_handle_mock_upload';
}
$size_check = self::check_upload_size( $files['file'] );
if ( is_wp_error( $size_check ) ) {
return $size_check;
}
// Include filesystem functions to get access to wp_handle_upload().
require_once ABSPATH . 'wp-admin/includes/file.php';
$file = wp_handle_upload( $files['file'], $overrides, $time );
if ( isset( $file['error'] ) ) {
return new WP_Error(
'rest_upload_unknown_error',
$file['error'],
array( 'status' => 500 )
);
}
return $file;
}