Advertisements

wp_upload_bits

Definition:
function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {}

Create a file in the upload folder with given content.
If there is an error, then the key ‘error’ will exist with the error message. If success, then the key ‘file’ will have the unique file path, the ‘url’ key will have the link to the new file. and the ‘error’ key will be set to false.

Parameters

  • string $name
  • null $deprecated: Never used. Set to null.
  • mixed $bits: File content
  • string $time: Optional. Time formatted in ‘yyyy/mm’.

Defined filters

  • wp_upload_bits
    apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time )

Source code

function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {

	if ( !empty( $deprecated ) )

		_deprecated_argument( __FUNCTION__, '2.0' );



	if ( empty( $name ) )

		return array( 'error' => __( 'Empty filename' ) );



	$wp_filetype = wp_check_filetype( $name );

	if ( !$wp_filetype['ext'] )

		return array( 'error' => __( 'Invalid file type' ) );



	$upload = wp_upload_dir( $time );



	if ( $upload['error'] !== false )

		return $upload;



	$upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) );

	if ( !is_array( $upload_bits_error ) ) {

		$upload[ 'error' ] = $upload_bits_error;

		return $upload;

	}



	$filename = wp_unique_filename( $upload['path'], $name );



	$new_file = $upload['path'] . "/$filename";

	if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {

		$message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), dirname( $new_file ) );

		return array( 'error' => $message );

	}



	$ifp = @ fopen( $new_file, 'wb' );

	if ( ! $ifp )

		return array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) );



	@fwrite( $ifp, $bits );

	fclose( $ifp );

	clearstatcache();



	// Set correct file permissions

	$stat = @ stat( dirname( $new_file ) );

	$perms = $stat['mode'] & 0007777;

	$perms = $perms & 0000666;

	@ chmod( $new_file, $perms );

	clearstatcache();



	// Compute the URL

	$url = $upload['url'] . "/$filename";



	return array( 'file' => $new_file, 'url' => $url, 'error' => false );

}

4249

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: