Welcome aboard! We are happy you are here and wish you good net-raft!
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<script>
$(document).ready(function () {
$("form").submit(function () {
$.ajax({
url: "page.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function (data) {
}
});
});
});
</script>
</body>
</html>
// this placed into php file
<?php
$sourcePath = $_FILES['fileToUpload']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "Images/".$_FILES['fileToUpload']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
?>
this is with callback:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" form="upl1" value="Upload Image" name="submit">
</form>
</p>
<form method="GET" id ="load1" action="/index.php"></form>
<div class="echo1"></div>
<?php
echo'<script type="text/javascript">$(document).ready(function(){$(".echo1").text("' . $_SESSION['status'] . '");});</script>';
?>
<script>
$(document).ready(function () {
$("form").submit(function () {
$.ajax({
url: "page.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function () {
$("#load1").submit();
}
});
});
});
</script>
</body>
</html>
// this placed into php file
<?php
session_start();
$sourcePath = $_FILES['fileToUpload']['tmp_name'];
$targetPath = "Images/" . $_FILES['fileToUpload']['name'];
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$FileType = pathinfo($targetPath,PATHINFO_EXTENSION);
if($iFileType != "jpg" && $FileType != "png" && $FileType != "jpeg" && $FileType != "gif" ) {
$_SESSION['status'] = "only jpg, png, jpeg, gif formats are allowed!";
}
else
{
if (move_uploaded_file($sourcePath,$targetPath)) {
$_SESSION['status'] = "uploaded";
} else {
$_SESSION['status'] = "Sorry, there was an error uploading your file.";
}
}
} else {
$_SESSION['status'] = "File is not an image.";
}
?>
The most helpful JQUERY solutions
plugin get client ip address using jqueryJQUERY Click to see more ... 10.4K 1.8K