Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting

WordPress Front End Multiple File Upload


While customizing Wordpress for our suitable cms solutions. We have certain things required from our customers. So it requires front end media upload. Even few things require to gather details and posts from users. If it requires you can also provide your user to upload media files from the front end of your site. May be of my following code can help you to create front end upload with multiple files simultaneously from single from . 

You don't need to use any of the following things : flash ,js,  jquery, and ajax . It works on all the browsers and simple HTML 5 codes and php code. This will work well on your old browsers like IE 6 . Also works well on your mobile devices. 

 create HTML form like this

<form action="upload.php" method="post" name="front_end_upload" >

 <label> Attach all your files here :<input type="file" name="upload_attachments[]"  multiple="multiple" > </label>

<input type="submit" name="Upload" >

</form>

The above HTML form handles the user upload files at your sites front-end. Now code for your back end to store the files.

//upload.php 
if( 'POST' == $_SERVER['REQUEST_METHOD']  ) {
if ( $_FILES ) { 
$files = $_FILES["upload_attachments"];  
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 ("screenshots" => $file); 
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid); 
}
}

}


The above code will handle the operation of getting files and send to your Wordpress function insert_attachment that we have added on your theme fiel "function.php" or your plugin's init action function.

here is the code for insert attachment function 

################################################################################
// image Attachment
################################################################################
function insert_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 ($set_thu) set_post_thumbnail($post_id, $attach_id);
return $attach_id;
}

that's it. to perform front end multiple file upload.


Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting