Last active
May 16, 2018 03:47
-
-
Save Manjulajntuk/86c3e616c68fa0ecc65ae4e299ae7a9e to your computer and use it in GitHub Desktop.
Drag and drop or onclick upload button has to work with ajax and php javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(isset($_FILES["file"]["type"])) | |
{ | |
$validextensions = array("jpeg", "jpg", "png"); | |
$temporary = explode(".", $_FILES["file"]["name"]); | |
$file_extension = end($temporary); | |
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") | |
) && ($_FILES["file"]["size"] < 10000000)//Approx. 100kb files can be uploaded. | |
&& in_array($file_extension, $validextensions)) { | |
if ($_FILES["file"]["error"] > 0) | |
{ | |
echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>"; | |
} | |
else | |
{ | |
if (file_exists("upload/" . $_FILES["file"]["name"])) { | |
echo $_FILES["file"]["name"] . " <span id='invalid'><b>already exists.</b></span> "; | |
} | |
else | |
{ | |
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable | |
$targetPath = "upload/".$_FILES['file']['name']; // Target path where file is to be stored | |
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file | |
echo "<span id='success'>Image Uploaded Successfully...!!</span><br/>"; | |
echo "<br/><b>File Name:</b> " . $_FILES["file"]["name"] . "<br>"; | |
echo "<b>Type:</b> " . $_FILES["file"]["type"] . "<br>"; | |
echo "<b>Size:</b> " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; | |
echo "<b>Temp file:</b> " . $_FILES["file"]["tmp_name"] . "<br>"; | |
} | |
} | |
} | |
else | |
{ | |
echo "<span id='invalid'>***Invalid file Size or Type***<span>"; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Ajax Image Upload Using PHP and jQuery</title> | |
<style> | |
o Condensed', sans-serif; | |
} | |
h1 | |
{ | |
text-align: center; | |
background-color: #FEFFED; | |
height: 70px; | |
color: rgb(95, 89, 89); | |
margin: 0 0 -29px 0; | |
padding-top: 14px; | |
border-radius: 10px 10px 0 0; | |
font-size: 35px; | |
} | |
.main { | |
position: absolute; | |
top: 50px; | |
left: 20%; | |
width: 450px; | |
height:530px; | |
border: 2px solid gray; | |
border-radius: 10px; | |
} | |
.main label{ | |
color: rgba(0, 0, 0, 0.71); | |
margin-left: 60px; | |
} | |
#image_preview{ | |
position: absolute; | |
font-size: 30px; | |
top: 100px; | |
left: 100px; | |
width: 250px; | |
height: 230px; | |
text-align: center; | |
line-height: 180px; | |
font-weight: bold; | |
color: #C0C0C0; | |
background-color: #FFFFFF; | |
overflow: auto; | |
} | |
#selectImage{ | |
padding: 19px 21px 14px 15px; | |
position: absolute; | |
bottom: 0px; | |
width: 414px; | |
background-color: #FEFFED; | |
border-radius: 10px; | |
} | |
.submit{ | |
font-size: 16px; | |
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%); | |
border: 1px solid #e5a900; | |
color: #4E4D4B; | |
font-weight: bold; | |
cursor: pointer; | |
width: 300px; | |
border-radius: 5px; | |
padding: 10px 0; | |
outline: none; | |
margin-top: 20px; | |
margin-left: 15%; | |
} | |
.submit:hover{ | |
background: linear-gradient(#ffdd7f 5%, #ffbc00 100%); | |
} | |
#file { | |
color: red; | |
padding: 5px; | |
border: 5px solid #8BF1B0; | |
background-color: #8BF1B0; | |
margin-top: 10px; | |
border-radius: 5px; | |
box-shadow: 0 0 15px #626F7E; | |
margin-left: 15%; | |
width: 72%; | |
} | |
#message{ | |
position:absolute; | |
top:120px; | |
left:815px; | |
} | |
#success | |
{ | |
color:green; | |
} | |
#invalid | |
{ | |
color:red; | |
} | |
#line | |
{ | |
margin-top: 274px; | |
} | |
#error | |
{ | |
color:red; | |
} | |
#error_message | |
{ | |
color:blue; | |
} | |
#loading | |
{ | |
display:none; | |
position:absolute; | |
top:50px; | |
left:850px; | |
font-size:25px; | |
} | |
.demo-droppable { | |
background: #08c; | |
color: #fff; | |
padding: 100px 0; | |
text-align: center; | |
} | |
.demo-droppable.dragover { | |
background: #00CC71; | |
} | |
</style> | |
<link rel="stylesheet" href="style.css" /> | |
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed|Open+Sans+Condensed:300' rel='stylesheet' type='text/css'> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="script.js"></script> | |
</head> | |
<body> | |
<div class="main"> | |
<h1>Ajax Image Upload</h1><br/> | |
<hr> | |
<form id="uploadimage" action="" method="post" enctype="multipart/form-data"> | |
<div class="demo-droppable"> | |
<p>Drag files here or click to upload</p> | |
</div> | |
<div class="output"></div> | |
<hr id="line"> | |
<div id="selectImage"> | |
<label>Select Your Image</label><br/> | |
<input type="file" name="file" id="file" required /> | |
<input type="submit" value="Upload" class="submit" /> | |
</div> | |
</form> | |
</div> | |
<h4 id='loading' >loading..</h4> | |
<div id="message"></div> | |
<script> | |
(function(window) { | |
function triggerCallback(e, callback) { | |
if(!callback || typeof callback !== 'function') { | |
return; | |
} | |
var files; | |
if(e.dataTransfer) { | |
files = e.dataTransfer.files; | |
} else if(e.target) { | |
files = e.target.files; | |
} | |
callback.call(null, files); | |
} | |
function makeDroppable(ele, callback) { | |
var input = document.createElement('input'); | |
input.setAttribute('type', 'file'); | |
input.setAttribute('multiple', true); | |
input.style.display = 'none'; | |
input.addEventListener('change', function(e) { | |
triggerCallback(e, callback); | |
}); | |
ele.appendChild(input); | |
ele.addEventListener('dragover', function(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
ele.classList.add('dragover'); | |
}); | |
ele.addEventListener('dragleave', function(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
ele.classList.remove('dragover'); | |
}); | |
ele.addEventListener('drop', function(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
ele.classList.remove('dragover'); | |
triggerCallback(e, callback); | |
}); | |
ele.addEventListener('click', function() { | |
input.value = null; | |
input.click(); | |
}); | |
} | |
window.makeDroppable = makeDroppable; | |
})(this); | |
(function(window) { | |
makeDroppable(window.document.querySelector('.demo-droppable'), function(files) { | |
console.log(files); | |
var output = document.querySelector('.output'); | |
output.innerHTML = ''; | |
for(var i=0; i<files.length; i++) { | |
if(files[i].type.indexOf('image/') === 0) { | |
output.innerHTML += '<img width="200" src="' + URL.createObjectURL(files[i]) + '" />'; | |
} | |
output.innerHTML += '<p>'+files[i].name+'</p>'; | |
} | |
}); | |
})(this); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function (e) { | |
$("#uploadimage").on('submit',(function(e) { | |
e.preventDefault(); | |
$("#message").empty(); | |
$('#loading').show(); | |
$.ajax({ | |
url: "ajax_php_file.php", // Url to which the request is send | |
type: "POST", // Type of request to be send, called as method | |
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values) | |
contentType: false, // The content type used when sending data to the server. | |
cache: false, // To unable request pages to be cached | |
processData:false, // To send DOMDocument or non processed data file it is set to false | |
success: function(data) // A function to be called if request succeeds | |
{ | |
$('#loading').hide(); | |
$("#message").html(data); | |
} | |
}); | |
})); | |
// Function to preview image after validation | |
$(function() { | |
$("#file").change(function() { | |
$("#message").empty(); // To remove the previous error message | |
var file = this.files[0]; | |
var imagefile = file.type; | |
var match= ["image/jpeg","image/png","image/jpg"]; | |
if(!((imagefile==match[0]) || (imagefile==match[1]) || (imagefile==match[2]))) | |
{ | |
$('#previewing').attr('src','noimage.png'); | |
$("#message").html("<p id='error'>Please Select A valid Image File</p>"+"<h4>Note</h4>"+"<span id='error_message'>Only jpeg, jpg and png Images type allowed</span>"); | |
return false; | |
} | |
else | |
{ | |
var reader = new FileReader(); | |
reader.onload = imageIsLoaded; | |
reader.readAsDataURL(this.files[0]); | |
} | |
}); | |
}); | |
function imageIsLoaded(e) { | |
$("#file").css("color","green"); | |
$('#image_preview').css("display", "block"); | |
$('#previewing').attr('src', e.target.result); | |
$('#previewing').attr('width', '250px'); | |
$('#previewing').attr('height', '230px'); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment