Ajax Image Upload using PHP and jQuery

Following steps are pointed under this process :

1. HTML page with upload field.

2.  Include jQuery library.

3.  jQuery Ajax code.

4.  PHP script to store image.

Git URL :

https://github.com/laddu1993/file-upload-using-php-ajax

Ajax Code :

$(“#loading”).hide();
$(‘#fileToUpload’).on(‘change’, function(e) {
e.preventDefault();
var formData = new FormData();
formData.append(‘file_image’, $(‘#wrapper input[type=file]’)[0].files[0]);
var image_data = file_browse(formData, ‘‘);
if (image_data.status == ‘success’) {
$(‘#result’).html(image_data.message);
$(‘#file_image’).val(image_data.web_url);
$(‘#show_image’).show();
$(‘#show_image img’).attr(“src”, image_data.web_url);
} else {
err_d = ‘Image is not Uploaded!’;
$(‘#result’).html(err_d);
}
});
function file_browse(formData, url) {
var result;
$.ajax({
type: ‘post’,
dataType: ‘json’,
data: formData,
enctype: ‘multipart/form-data’,
contentType: false,
cache: false,
processData: false,
async: false,
url: url,
beforeSend: function(xhr) {
$(“#loading”).show();
},
success: function(data) {
$(“#loading”).hide();
result = data;
},
error: function(data) {
result = data;
}
});
return result;
}

PHP code used to store the image :

$save_path = dirname(__FILE__).”/uploads/”;
$actual_url = (isset($_SERVER[‘HTTPS’]) ? “https” : “http”) . “://$_SERVER[HTTP_HOST]”;
$actual_url .= (isset($_SERVER[‘REQUEST_URI’]) ? $_SERVER[‘REQUEST_URI’] : ”);
$actual_url = explode(“/”, $actual_url);
array_pop($actual_url);
$actual_url = implode(“/”, $actual_url).’/uploads/’;
if (!empty($_FILES) && isset($_FILES)) {
$rand = rand();
$image_name = $rand.’_’.$_FILES[‘file_image’][‘name’];
$tmp_image_name = $_FILES[‘file_image’][‘tmp_name’];
$image_type = $_FILES[‘file_image’][‘type’];
$image_size = $_FILES[‘file_image’][‘size’];
$status = move_uploaded_file($tmp_image_name, $save_path.$image_name);
$file_arr = array(‘web_url’ => $actual_url.$image_name, ‘status’ => ‘success’, ‘message’ => ‘Image uploaded successfully’, ‘file_name’ => $image_name);
echo json_encode($file_arr);
exit();
}

JQUERY LIBRARAY:

https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js

HTML CODE:

FILE UPLOAD USING PHP AJAX

Image Upload

Conclusion :
In this blog post we have covered a basic topic of ajax of uploading image using PHP. Hope this might have worked for you to understand the ajax property. Keep reading our other blog posts for more interesting.

How To Post Instagram Photos From A Desktop Web Browser

A recent update to Instagram’s website has just enabled a feature which users of iPads and desktop PCs everywhere have been crying out for since the start. You can now upload images directly from a browser without the need to use the Instagram app and it’s trivially easy for Chrome users.

Continue reading “How To Post Instagram Photos From A Desktop Web Browser”

Stored procedure in codeigniter

Steps To Call Stored Procedure In Codeigniter:

The way you call a stored procedure can depend on how you create it. It’s time to discuss three common ways to create and call a stored procedure in CodeIgniter.

1. Create a stored procedure for select operation.


CREATE PROCEDURE Select_first ()
BEGIN
SELECT * FROM table_name;
END

Now that the Select_first procedure has been created and defined, we can call this procedure at any time in CodeIgniter. In this example, we create a variable, $data, by pointing to the procedure above after loading and initializing the database class in CodeIgniter.

To call this specific procedure within a query:-

       $data = $this->db->query(“CALL Select_first ()”);
       mysqli_next_result($this->db->conn_id);
       $result = $data->result();

Continue reading “Stored procedure in codeigniter”

Mumbai University stops BSc IT course

The University of Mumbai (MU) has decided to close the BSc Information Technology (IT) course that the university’s department of IT runs.

The course was one of the few undergraduate courses offered by the varsity.

The decision, that is said to allow the varsity to focus on PG education and research, has instigated protests who said that it will be a loss for students.

Continue reading “Mumbai University stops BSc IT course”

PHP Web Constellations

PHP is one of the best choice for creating web applications. Because it supports advanced web constellations. It supports most of the popular databases, libraries. PHP script can be embedded  with hypertext and client-side script.

With these collective integration, PHP as an open-source attracts people in web development businesses. Currently, most renowned websites are using PHP. For example, Google, Facebook, Yahoo! and more.  As per 2013 metrics made by online research agencies like Netcraft, more than 200 million websites is using PHP.