I'm trying to upload a file using XMLHttpRequest() function to run a php script . Here is the HTML code
<form enctype="multipart/form-data" action="testupload.php" method="POST">
<input type="text" class="form-control input-lg" name="image_text" id="filename">
<input type="hidden" name="MAX_FILE_SIZE" value="51200000000" />
Send this file: <input name="usrfile" type="file" />
<input type="button" value="Send File" onclick="uploadtest()"/>
</form>
Here is javascript code
function uploadtest(){
var file2 =document.getElementById("filename").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xmlhttp.open("GET", "testupload.php?fileID=" + file2 , true);
xmlhttp.send();
}
This is my PHP script
<?php
/if(!empty($_FILES['usrfile'])){
// print_r($_FILES);
// echo "goda";
// // File upload configuration
$targetDir = "C:/wamp64/www/CMS_TEST2/uploads/";
$allowTypes = array('mp4', 'jpg', 'png', 'jpeg', 'gif');
// $fileName = basename($_FILES['file']['name']);
$fileName = $_GET["fileID"];
// $fileName = "MCCT0001";
$temp = explode(".", $_FILES["usrfile"]["name"]);
$newfilename = $fileName.".".end($temp);
$targetFilePath = $targetDir.$newfilename;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to the server
if(move_uploaded_file($_FILES['usrfile']['tmp_name'], $targetFilePath)){
echo "File is valid, and was successfully uploaded.\n";
print_r($_FILES);
}
}
//}
?>
If i call the php script using PHP isset form submit. file upload works. But the issue when i call the php script using ajax $_FILES returns empty. Can you please help me with what i'm doing wrong here