Reputation: 571
I have been trying for hours to get this working.
WP image upload works fine for single image without using multiple
tag.
But when tried to to upload multiple images after changing the php code for multiple images (array) and adding multiple
tag, show error:
File upload data is coming through JSON which works fine when printed.
Below is what I have tried so far:
if(!function_exists('wp_handle_upload')){
require_once(ABSPATH.'wp-admin/includes/file.php');
}
foreach($styledNameData as $nameData){
//echo '<pre>'; print_r($nameData);
// It Prints
//Array
//(
// [name] => A_Front.png
// [type] => image/png
// [tmp_name] => C:\\xampp\\tmp\\php5BFB.tmp
// [error] => 0
// [size] => 32901
//)
$upload_overrides = array('test_form' => false);
$movefile = wp_handle_upload($nameData, $upload_overrides);
}
if($movefile && !isset($movefile['error'])){
echo '<pre>';
print_r($movefile);
} else {
echo '<pre>';
print_r($movefile['error']);
}
HTML is:
<form id="imagesForm" method="POST" enctype="multipart/form-data" action="<?php echo admin_url('admin-ajax.php'); ?>">
<input id="uploadfile" type="file" class="button button-secondary" name="uploadfile[]" multiple />
<input type="hidden" name="action" value="shpg_image_upload" />
<input type="submit" id="uploadImages" name="uploadImages" class="btn-medium col-sms-5 col-sm-3" value="UPLOAD" />
</form>
Please help.
Upvotes: 2
Views: 3270
Reputation: 776
I ran into the same issue today. The accepted answer isn't really correct. If you look at wp-admin/includes/ at the function in question _wp_handle_upload, the check that fires this error is simply a check for is_uploaded_file - whether the file exists. Testing for file size and type doesn't happen until after. So no - the solution if you're getting this message won't be changing those parameters.
I ended up debugging this by looking at the $_FILES array when it comes back from my form, inside my admin_post action. The way you have your form setup, I assume it will be the same as mine. The $_FILES array is not in the format that is expected here, and because wp_handle_upload is being passed an index to the $_FILES array - the $file variable at the point in the code where this error fires is NULL if you dump it out (file.php:808).
My $_FILES array looked like this:
array(1) {
["field_name"]=> array(5) {
["name"]=> array(2) {
[0]=>string(10) "1.png"
[1]=>string(10) "2.png"
}
["type"]=> array(2) {
[0]=>string(9) "image/png"
[1]=>string(9) "image/png"
}
["tmp_name"]=> array(2) {
[0]=>string(26) "/private/var/tmp/phpgg1rgH"
[1]=>string(26) "/private/var/tmp/phpMe907s"
}
["error"]=> array(2) {
[0]=>int(0)
[1]=>int(0)
}
["size"]=> array(2) {
[0]=>int(928815)
[1]=>int(871340)
}
}
}
So as you can see the properties of each file are broken up firstly by the property - not by the file index. Now if you change the structure of the files array like so:
$images = [];
foreach( $_FILES[ 'field_name' ] as $prop => $values ){
foreach( $values as $key => $value ){
$images[ $key ][ $prop ] = $value;
}
}
$_FILES = $images;
BEFORE you call wp_handle_upload() - everything should work like you expect. That was my issue anyway - not sure if there's a better way to go about solving that error. I have to think this can be solved somehow at the form level rather that overwriting the $_FILES itself. But in a pinch - it fixed my issue.
Upvotes: 1
Reputation: 623
This can often happen if the $_FILES array is empty. The $_FILES array can be empty for a couple different reasons and more often than not it is because one of the following server settings needs to be changed.
file_uploads=Off <--- Set this to On
post_max_size=8M <-- increase this to be larger than your image
upload_max_filesize=2M <-- increase this to be larger than your image
Upvotes: 0