uploading files outside of wordpress admin

if i have 4 images (called add_image1, add_image2, add_image3 and add_image4) to be uploaded outside of wordpress admin and wants to use wordpress file uploading functionality,

if (isset($_FILES)) {
for ($i = 0; $i < 4; $i ++) { $name = ‘add_image’.$i; if ($_FILES[$name][‘size’][$k] > 2000000) {
exit(‘Each image uploaded must be less than 2 Megabytes.’);
}
elseif (!$_FILES[$name][‘error’]) {
require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);
$id = media_handle_sideload( $_FILES[$name], $post_id, ‘uploaded image ‘.$i );
$content .= ‘<img src=”‘.wp_get_attachment_url($id).'” alt=”” />’;
}
}
}

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.