WP_REST_Attachments_Controller::check_upload_size

Advertisement

Summery Summery

Determine if uploaded file exceeds space quota on multisite.

Syntax Syntax

WP_REST_Attachments_Controller::check_upload_size( array $file )

Description Description

Replicates check_upload_size().

Parameters Parameters

$file

(array) (Required) $_FILES array for a given file.

Return Return

(true|WP_Error) True if can upload, error for errors.

Source Source

File: wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

		// Get the content-type.
		$type = array_shift( $headers['content_type'] );

		// Include filesystem functions to get access to wp_tempnam() and wp_handle_sideload().
		require_once ABSPATH . 'wp-admin/includes/file.php';

		// Save the file.
		$tmpfname = wp_tempnam( $filename );

		$fp = fopen( $tmpfname, 'w+' );

		if ( ! $fp ) {
			return new WP_Error(
				'rest_upload_file_error',
				__( 'Could not open file handle.' ),
				array( 'status' => 500 )
			);
		}

		fwrite( $fp, $data );
		fclose( $fp );

		// Now, sideload it in.
		$file_data = array(
			'error'    => null,
			'tmp_name' => $tmpfname,
			'name'     => $filename,
			'type'     => $type,
		);

		$size_check = self::check_upload_size( $file_data );
		if ( is_wp_error( $size_check ) ) {
			return $size_check;
		}

		$overrides = array(
			'test_form' => false,
		);

		$sideloaded = wp_handle_sideload( $file_data, $overrides );

		if ( isset( $sideloaded['error'] ) ) {
			@unlink( $tmpfname );

Advertisement

Changelog Changelog

Changelog
Version Description
4.9.8 Introduced.

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.