0

So, I'm uploading an image file which works with the following code however, it does not appear in the Media Library. I get a "File is valid, and was successfully uploaded" message but I must be missing a step.

if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

$files = array('tmp_name' => $_FILES[$fieldname]['tmp_name'],
            'name' => $_FILES[$fieldname]['name']);



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

$movefile = @wp_handle_upload( $files, $overrides );

if ( $movefile && !isset( $movefile['error'] ) ) {
echo "File is valid, and was successfully uploaded.\n";

} else {
/**
 * Error generated by _wp_handle_upload()
 * @see _wp_handle_upload() in wp-admin/includes/file.php
 */
echo $movefile['error'];
0

The missing element that makes a file appear in the media library is the attachment post. Attachment posts hold the file metadata, and are what get associated to posts via the post_parent when a file is "attached".

Instead of wp_handle_upload, you can use the function media_handle_upload, which will handle the file upload as well as insert the attachment post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.