• Resolved edtiley

    (@edtiley)


    I’m uploading images to a multisite network using cordova-plugin-file-transfer using the following:

    	function get_file() {
    	
    if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
    
    $uploadedfile = $_FILES['file'];
    
    $upload_overrides = array( 'test_form' => false );
    
    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    
    if ( $movefile && ! isset( $movefile['error'] ) ) {
        echo "File is valid, and was successfully uploaded.\n";
        echo $_FILES[file]['who']  . '**************************************************************\n';
        
        var_dump( $movefile );
    } else {
        /**
         * Error generated by _wp_handle_upload()
         * @see _wp_handle_upload() in wp-admin/includes/file.php
         */
        echo $movefile['error'];
    }
    }
    	 

    The image is being written to the proper place in upload directory by wp_handle_upload, but doesn’t show up as part of the media library.

    Anyone have a clue why the image isn’t being shown in the library?

Viewing 7 replies - 1 through 7 (of 7 total)
  • It’s not showing up in the media library because you haven’t saved the images attachment post object, just moved the file to the right location.

    As a sample, this is some code that I use for that.

    $upload_overrides = array( "test_form" => false );
    
    $uploaded_file = wp_handle_upload ($file, $upload_overrides);
    
    if( isset( $uploaded_file ["file"] )) {
    	$file_name_and_location = $uploaded_file ["file"];
    	$file_title_for_media_library = $title;
    
    	$attachment = array(
    		"post_mime_type" => $uploaded_file_type,
    		"post_title" => addslashes( $file_title_for_media_library ),
    		"post_content" => "",
    		"post_status" => "inherit"
    	);
    
    	if( ! is_null( $post )) {
    		if ( ! is_numeric( $post )) {
    			$post = $post->ID;
    		} // if ()
    
    		$attachment ['post_parent'] = $post;
    	} // if ()
    
    	$id = wp_insert_attachment( $attachment, $file_name_and_location );
    
    	require_once( ABSPATH."wp-admin/includes/image.php" );
    
    	$attach_data = wp_generate_attachment_metadata( $id, $file_name_and_location );
    	wp_update_attachment_metadata( $id, $attach_data );
    } // if ()
    Thread Starter edtiley

    (@edtiley)

    Oh!!! Excellent, I’ve added the code to my file and will check it out in the AM. Thank you.

    Now all I have to do is figure out how to capture the params (an array of key/value pairs embeded in $_FILES) from from the Cordova File Transfer plugin, and I’ll have this thing worked out.

    When I get it, I’ll post the code here.

    Thanks again.

    Thread Starter edtiley

    (@edtiley)

    Well I had to do a little tweaking, and they show up in the media library, but only as icons. They are apparently not being processed to create thumbnails, etc. I’ve looked into the posts and post_meta tables and things look to be “normal,” but these days who can say what is normal?

    Here’s my amended code”

    
    function get_file() {
    	
    if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
    
    if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) require_once( ABSPATH . 'wp-admin/includes/image.php' );
    
    $uploadedfile = $_FILES['file'];
    
    $upload_overrides = array( 'test_form' => false );
    
    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    
    if( isset( $movefile["file"] )) {
    	$file_name_and_location = $movefile["file"];
    	$file_title_for_media_library = $_POST['who'];
       $wp_upload_dir = wp_upload_dir();
    	$filetype = wp_check_filetype( $uploadedfile );
    	
    	
    	$attachment = array(
    		"guid" => $movefile['file'],
    		"post_mime_type" => $filetype['type'],
    		"post_title" => addslashes( $file_title_for_media_library ),
    		"post_content" => "",
    		"post_status" => "inherit",
    		"post_author" => 1
    	);
    
    	if( ! is_null( $post )) {
    		if ( ! is_numeric( $post )) {
    			$post = $post->ID;
    		} // if ()
    
    		$attachment ['post_parent'] = $post;
    	} // if ()
    
    	require_once( ABSPATH."wp-admin/includes/image.php" );
    	
    	$id = wp_insert_attachment( $attachment, $file_name_and_location );
    
    	$attach_data = wp_generate_attachment_metadata( $id, $file_name_and_location );
    	wp_update_attachment_metadata( $id, $attach_data );
    	}
    }
    	 

    BTW, when uploading from the Cordoba File Transfer plugin, the params show up in $_POST

    Moderator bcworkz

    (@bcworkz)

    wp_generate_attachment_metadata() normally generates intermediate image sizes. (at line 135, $editor->multi_resize( $sizes );

    Do intermediate image sizes occur where the uploaded file finally resides? If not, there may be something about the images that the image editor cannot handle.

    If there are intermediate sizes generated, but you still get icons, the problem is likely that image_downsize() is failing to return a valid image path. This should work if you have intermediate image sizes, as it merely selects the best size to use in the media library. But this failure is what causes icons to display. This behavior is defined in wp_get_attachment_image_src(), which will return icon URLs if no appropriate image is found.

    I hope this helps. While I have no direct answer for your issue, I do know where to look for likely problems.

    Thread Starter edtiley

    (@edtiley)

    bcworkz –

    No. there are no thumbnails or other image sizes generated. The file is sent from an android device via cordova file transfer. If I click on the icon in the media library, I get a link to view the attachment page, and when I clik on that link, the image displays just fine. I would assume that means it is just a plain old jpg file.

    There has to be some parameter my code is not passing correctly.

    The site is Multisite, but the file is showing up in the proper sites folder and everything.

    Thread Starter edtiley

    (@edtiley)

    catacaustic – In your example, where does $post variable come from?

    Thread Starter edtiley

    (@edtiley)

    Catacaustic – bcworks

    The section of the example below if( ! is_null( $post )) is what lead me astray. The resolution is a simple passing of the right info to the attachment functions. (as it usually is!) This is the code that works:

    
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    	require_once( ABSPATH . 'wp-admin/includes/file.php' );
    
    $uploadedfile = $_FILES['file'];
    
    $upload_overrides = array( 'test_form' => false );
    
    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    $wp_upload_dir = wp_upload_dir();
    
    	$attachment = array(
    		"guid" => $movefile['file'], 
    		"post_mime_type" => $movefile['type'],
    		"post_title" => $_POST['who'],
    		"post_content" => "",
    		"post_status" => "draft",
    		"post_author" => 1
    	);
    
    	$id = wp_insert_attachment( $attachment, $movefile['file'],0);
    
    	$attach_data = wp_generate_attachment_metadata( $id, $movefile['file'] );
    	wp_update_attachment_metadata( $id, $attach_data );
    

    Thanks for steering me in the right direction!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘wp_handle_upload() works, but image not in media library’ is closed to new replies.