Wordpress - Upload Multiple Files With media_handle_upload
here if you use custom template past this in the begining
<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
if ( $_FILES ) {
$files = $_FILES["my_file_upload"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_file_upload" => $file);
foreach ($_FILES as $file => $array) {
$newupload = my_handle_attachment($file,$pid);
}
}
}
}
}
?>
in function.php
function my_handle_attachment($file_handler,$post_id,$set_thu=false) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ( is_numeric( $attach_id ) ) {
update_post_meta( $post_id, '_my_file_upload', $attach_id );
}
return $attach_id;
}
soure http://www.kvcodes.com/2013/12/create-front-end-multiple-file-upload-wordpress/
If you want to implement this without using the functions file, you can use the streamlined version I came up with. This is the tested code works 100%
<form id="file_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="my_file_upload[]" multiple="multiple">
<input name="my_file_upload" type="submit" value="Upload" />
</form>
Place the PHP code on the page where the submit happens. In my case on the same page as the form action is set to "#"
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$files = $_FILES["my_file_upload"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_file" => $file);
$attachment_id = media_handle_upload("upload_file", 0);
if (is_wp_error($attachment_id)) {
// There was an error uploading the image.
echo "Error adding file";
} else {
// The image was uploaded successfully!
echo "File added successfully with ID: " . $attachment_id . "<br>";
echo wp_get_attachment_image($attachment_id, array(800, 600)) . "<br>"; //Display the uploaded image with a size you wish. In this case it is 800x600
}
}
}
} ?>
This method will include the required files only once when the form submit is done instead of including them each time the function is called through foreach loop
Thanks @shady-m-rasmy I used the code you mentionned, and it seems that the second foreach loop (below - in the custom template part) is not necessary as it is only executed once.
foreach ($_FILES as $file => $array) {
$newupload = my_handle_attachment($file,$pid);
}
So it only leaves with
$newupload = my_handle_attachment( "my_file_upload", $pid);