JQUERY
June 3, 2026 1 min read

Upload file or image using jQuery AJAX with PHP

HS
HEMANT SINGH
Technical Writer & Educator
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

	<form>
		<p>
			<label for="image">Image</label>
			<input type="file" class="image">
		</p>
		<p>
			<button type="button" class="upload_button">Upload</button>
		</p>
	</form>
	
</body>
</html>
<script type="text/javascript">
	$(function(){
		$(document).on("click",".upload_button", function(){
			var image = $(".image")[0].files[0];
			var form = new FormData();
			form.append("image", image);
			
			$.ajax({
				url:"server.php",
				method:"POST",
				// do not allow browser to process data
				processData: false,
				// allow browser to set the content type
				contentType: false,
				data:form,
				beforeSend:function(){

				},
				success:function(data){
					console.log(data);
				}
			})
		});
	});
</script>
<?php 
if(isset($_FILES)){
	$name = $_FILES["image"]["name"];
	$tmp_name = $_FILES["image"]["tmp_name"];
	$size = $_FILES["image"]["size"];
	$path="images/$name";	
	move_uploaded_file($tmp_name, $path);
	echo "Uploaded";
}
 ?>