How to upload file in wordpress front end side ajax

Posted: July 23, 2015 in Uncategorized
Tags: , , , , ,

HTML CODE

<code>

<input id=”cropContaineroutput” type=”file” name=”img” />

<input type=”text” id=”cropOutput” style=”width:100%; padding:5px 4%; margin:20px auto; display:block; border: 1px solid #CCC;” />
</code>

JQuery

<script>
var sofg_mixup = jQuery.noConflict();
//    window.onload = function(sofg_mixup) {
sofg_mixup(document).ready(function(){

sofg_mixup(‘#cropContaineroutput’).change(function(){

var pictureInput = sofg_mixup(‘#cropContaineroutput’).prop(‘files’)[0];
var myFormData = new FormData();
myFormData.append(‘img’, pictureInput);
sofg_mixup.ajax({
url: ‘<?php echo get_template_directory_uri() ?>/img_save_to_file.php’,
type: ‘POST’,
processData: false, // important
contentType: false, // important
dataType : ‘json’,
data:  myFormData,
success: function(jsonData){
alert(jsonData.status);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}
});
});
});

</script>

PHP

img_save_to_file.php (in theme folder)

<?php
require_once(‘../../../wp-load.php’);
//print_r($_FILES) ;

if (isset($_FILES[‘img’] ) && !empty($_FILES[‘img’][‘name’]) )
{
$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”, “GIF”, “JPEG”, “JPG”, “PNG”);
$temp = explode(“.”, $_FILES[“img”][“name”]);
$extension = end($temp);
if ( in_array($extension, $allowedExts))
{
if ($_FILES[“img”][“error”] > 0)
{
$response = array(
“status” => ‘error’,
“message” => ‘ERROR Return Code: ‘. $_FILES[“img”][“error”],
);
}
else
{
$uploadedfile = $_FILES[‘img’];
$upload_name = $_FILES[‘img’][‘name’];
$uploads = wp_upload_dir();
$filepath = $uploads[‘path’].”/$upload_name”;

if ( ! function_exists( ‘wp_handle_upload’ ) )
{
require_once( ABSPATH . ‘wp-admin/includes/file.php’ );
}
require_once( ABSPATH . ‘wp-admin/includes/image.php’ );
$upload_overrides = array( ‘test_form’ => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && !isset( $movefile[‘error’] ) ) {

$file = $movefile[‘file’];
$url = $movefile[‘url’];
$type = $movefile[‘type’];

$attachment = array(
‘post_mime_type’ => $type ,
‘post_title’ => $upload_name,
‘post_content’ => ‘Image for ‘.$upload_name,
‘post_status’ => ‘inherit’
);

$attach_id=wp_insert_attachment( $attachment, $file, 0);
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );

}

$response = array(
“status” => ‘success’,
“url” => $url.$_FILES[“img”][“name”]
);

}
}
else
{
$response = array(
“status” => ‘error’,
“message” => ‘something went wrong, most likely file is to large for upload. check upload_max_filesize, post_max_size and memory_limit in you php.ini’,
);
}
}
print json_encode($response);
exit;

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s