wp_upload_bits

Create a file in the upload folder with given content.

Description

(array) wp_upload_bits( (string) $name, (null|string) $deprecated, (mixed) $bits, (null) $time = null ); 

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.

This function will not move an uploaded file to the upload folder. It will create a new file with the content in $bits parameter. If you move the upload file, read the content of the uploaded file, and then you can give the filename and content to this function, which will add it to the upload folder.

The permissions will be set on the new file automatically by this function.

Returns (array)

Parameters (4)

0. $name (string)
The name.
1. $deprecated (null|string)
Never used. Set to null.
2. $bits (mixed)
File content
3. $time — Optional. (null) => null
Time formatted in yyyy/mm.. Default null.

Usage

  1. if ( !function_exists( 'wp_upload_bits' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // The name. 
  5. $name = ''; 
  6.  
  7. // Never used. Set to null. 
  8. $deprecated = null; 
  9.  
  10. // File content 
  11. $bits = null; 
  12.  
  13. // Optional. Time formatted in 'yyyy/mm'. Default null. 
  14. $time = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wp_upload_bits($name, $deprecated, $bits, $time); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_upload_bits( $name, $deprecated, $bits, $time = null ) { 
  2. if ( !empty( $deprecated ) ) 
  3. _deprecated_argument( __FUNCTION__, '2.0.0' ); 
  4.  
  5. if ( empty( $name ) ) 
  6. return array( error => __( 'Empty filename' ) ); 
  7.  
  8. $wp_filetype = wp_check_filetype( $name ); 
  9. if ( ! $wp_filetype['ext'] && ! current_user_can( 'unfiltered_upload' ) ) 
  10. return array( error => __( 'Invalid file type' ) ); 
  11.  
  12. $upload = wp_upload_dir( $time ); 
  13.  
  14. if ( $upload[error] !== false ) 
  15. return $upload; 
  16.  
  17. /** 
  18. * Filters whether to treat the upload bits as an error. 
  19. * Passing a non-array to the filter will effectively short-circuit preparing 
  20. * the upload bits, returning that value instead. 
  21. * @since 3.0.0 
  22. * @param mixed $upload_bits_error An array of upload bits data, or a non-arrayerrorto return. 
  23. */ 
  24. $upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) ); 
  25. if ( !is_array( $upload_bits_error ) ) { 
  26. $upload[ error ] = $upload_bits_error; 
  27. return $upload; 
  28.  
  29. $filename = wp_unique_filename( $upload['path'], $name ); 
  30.  
  31. $new_file = $upload['path'] . "/$filename"; 
  32. if ( ! wp_mkdir_p( dirname( $new_file ) ) ) { 
  33. if ( 0 === strpos( $upload['basedir'], ABSPATH ) ) 
  34. $error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir']; 
  35. else 
  36. $error_path = basename( $upload['basedir'] ) . $upload['subdir']; 
  37.  
  38. $message = sprintf( 
  39. /** translators: %s: directory path */ 
  40. __( 'Unable to create directory %s. Is its parent directory writable by the server?' ),  
  41. $error_path 
  42. ); 
  43. return array( error => $message ); 
  44.  
  45. $ifp = @ fopen( $new_file, 'wb' ); 
  46. if ( ! $ifp ) 
  47. return array( error => sprintf( __( 'Could not write file %s' ), $new_file ) ); 
  48.  
  49. @fwrite( $ifp, $bits ); 
  50. fclose( $ifp ); 
  51. clearstatcache(); 
  52.  
  53. // Set correct file permissions 
  54. $stat = @ stat( dirname( $new_file ) ); 
  55. $perms = $stat['mode'] & 0007777; 
  56. $perms = $perms & 0000666; 
  57. @ chmod( $new_file, $perms ); 
  58. clearstatcache(); 
  59.  
  60. // Compute the URL 
  61. $url = $upload['url'] . "/$filename"; 
  62.  
  63. /** This filter is documented in wp-admin/includes/file.php */ 
  64. return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], error => false ), 'sideload' );