humanmade / S3-Uploads

The WordPress Plugin to Store Uploads on Amazon S3
1.94k stars 391 forks source link

"Specified file failed upload test" when trying to sideload an image wrapped in an AJAX call #386

Open jmarx opened 4 years ago

jmarx commented 4 years ago

Has anyone else seen an issue when trying to use media_sideload_image inside a wp ajax call in conjunction with this plugin?

I tried even the most simple image upload inside a barebones ajax call.

require_once ABSPATH . 'wp-admin/includes/media.php';
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once ABSPATH . 'wp-admin/includes/image.php';

        $attach_id = media_sideload_image('https://s.w.org/about/images/logos/wordpress-logo-stacked-rgb.png', 0 );

I keep getting this error: 'Specified file failed upload test. https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/file.php#L815

The only way I can get it to work is to remove this filter: remove_action( 'wp_handle_sideload_prefilter', array( $s3, 'filter_sideload_move_temp_file_to_s3' which of course defeats the purpose of using the plugin in the first place.

@rmccue Do you have any thoughts on this?

FYI, the image uploads work in every use case except for the ajax call.

Thanks in advance for any feedback you guys may be able to provide!

rmccue commented 4 years ago

I don't think we've seen any issues with sideloading images at all, at least not to my knowledge.

In this case, it seems like the issue is from is_readable( $file['tmp_name'] ) failing. That tmp_name should be set to a path returned from download_url() which uses wp_tempnam() and get_temp_dir(). The temp directory isn't affected by S3 Uploads, so it's possible this is server configuration on your side; can you check what get_temp_dir() returns and whether that location is writable?

jmarx commented 4 years ago

@rmccue '/tmp/' I am getting this in response from that function. I should add that these failures only occur during AJAX calls. We experience no issues when using this function in any other context.

rmccue commented 4 years ago

Very strange.

I wonder if S3 Uploads is moving the file too early? If you can dig in and debug what's happening when it runs that check, please do (removing the @ might reveal some silenced errors from S3 Uploads too).