insert file and image usin php and ajax
HTML AND AJAX CODE
index.php
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<script>
function uploadFile(){
var input = document.getElementById("file");
file = input.files[0];
if(file != undefined){
formData= new FormData();
if(!!file.type.match(/application.*/)){
formData.append("application", file);
$.ajax({
url: "upload.php",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(data){
alert('success');
}
});
}else{
alert('Not a valid image!');
}
}else{
alert('Input something!');
}
}
</script>
<body>
<input type="file" id="file" />
<button onClick="uploadFile();">UploadFile</button>
</body>
</html>
Upload.php
<?php
$dir = "../gurukul/assignments/";
move_uploaded_file($_FILES["application"]["tmp_name"], $dir. $_FILES["application"]["name"]);
?>
HTML AND AJAX CODE
index.php
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<script>
function uploadFile(){
var input = document.getElementById("file");
file = input.files[0];
if(file != undefined){
formData= new FormData();
if(!!file.type.match(/application.*/)){
formData.append("application", file);
$.ajax({
url: "upload.php",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(data){
alert('success');
}
});
}else{
alert('Not a valid image!');
}
}else{
alert('Input something!');
}
}
</script>
<body>
<input type="file" id="file" />
<button onClick="uploadFile();">UploadFile</button>
</body>
</html>
Upload.php
<?php
$dir = "../gurukul/assignments/";
move_uploaded_file($_FILES["application"]["tmp_name"], $dir. $_FILES["application"]["name"]);
?>
No comments:
Post a Comment